Skip to content

Bug: CC0039 False positive when concatenating to loop variable propery/field (StringBuilderInLoop) #797

@RachelDavids

Description

@RachelDavids

This should not raise a diagnostic:

    class MyObject
    {
        public string MyObjectString;
    }
    class MyClass
    {
        private readonly List<MyObject> _items = new List<MyObject>
        {
                new MyObject {MyObjectString = "ItemA"},
                new MyObject {MyObjectString = "ItemB"},
                new MyObject {MyObjectString = "ItemC"}
        };
        private void MyMethod(string suffix)
        {
            foreach (MyObject o in _items)
            {
                o.MyObjectString += suffix; // CC0039 fires on this line
                o.MyObjectString = o.MyObjectString + suffix; // but not on this one
            }
        }
    }

The code fix makes no sense at all in this context.
The same issue occurs in a VB equivalent situation

The analyzer should check if the symbol being used is in context only within the foreach. Otherwise, it makes no sense.

The PR should go into the v1.0.x branch.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions