Rule ID: S1848 (C#) false positive, "Objects should not be created to be dropped immediately without being used"

119 views
Skip to first unread message

b...@root.ch

unread,
Dec 4, 2017, 10:19:50 AM12/4/17
to SonarQube
Hello,

We have some widely used components in our project that look similar to this:

public class Foo {
  public List<Bar> Bars;
}

public class Bar {
  public Bar(Foo foo) {
    foo.Bars.Add(this);
  }
}

...
Foo foo = new Foo();
new Bar(foo); // -> S1848

This leads to many issues (bugs) of type "Objects should not be created to be dropped immediately without being used" (about 4K).

I agree that this code is at least a little smelly. Nevertheless the issue is a false positive as the object is in fact being used.

Would it be feasibile to check for this 'pattern'? The example is simplified, in some cases there is a rather large inheritance chain...

Thanks a lot for considering

Regards 
Michael

valeri....@sonarsource.com

unread,
Dec 4, 2017, 11:35:34 AM12/4/17
to SonarQube
Hi Michael,

SonarC# would have been able to detect this false positive if it supported cross-procedural analysis, but it does not yet (e.g. if it "understood" what the called method or constructor is doing). We have this feature in our backlog, but we are still far from implementing it.

For the time being you could use the bulk editing in SonarQube to mark all these issues as False Positive. If you are using Connected Mode, the latest versions of SonarLint would be able to suppress the closed issues and they will not bother your while you are working (for the price of a minute or two delay when the first issues appear, but only the first time you open a solution; the subsequent analyses would be fine).

Other option would be to just disable the rule, but that's usually less than ideal.

All the best,
Valeri Hristov

 

b...@root.ch

unread,
Dec 5, 2017, 11:13:33 AM12/5/17
to SonarQube
Hi Valeri,

Thank you for your answer. I used bulk editing to mark them as false positive.

Regards
Michael
Reply all
Reply to author
Forward
0 new messages