Sonar-dotnet: Fix S2325: Should raise issues for methods and properties with SuppressMessage attribute

Created on 1 Aug 2017  路  2Comments  路  Source: SonarSource/sonar-dotnet

Description

Methods and properties with an attribute do not raise issues.

ORIGINAL: SuppressMessage with nonexistent category and check ID still suppresses issues.

Repro steps

Define the following method and make sure S2325: Methods and properties that don't access instance data should be static is enabled:

private int Get()
{
    return 42;
}

Note S2325 is reported. Now add the following suppression attribute:

[System.Diagnostics.CodeAnalysis.SuppressMessage("Foo", "Bar", Justification = "baz")]
private int Get()
{
    return 42;
}

EDIT: Any attribute breaks the rule.

Expected behavior

The issue should remain, since the proper category (pitfall?) and check ID (S2325) weren't supplied.

Actual behavior

The issue is suppressed.

Known workarounds

Use #pragma warning disable / #pragma warning restore

Related information

  • SonarC# Version 6.2.0.2536
  • Visual Studio Version 2017 (15.2, 26430.16)
Bug

All 2 comments

Thanks for your feedback! I was able to reproduce the problem, the fix will be included in the next version of SonarC#.

We decided to allow raising issues only if there is a SuppressMessage attribute. If other attributes are present we will not raise in order to reduce the false positives when using attribute-based functionality such as serialization, WCF, etc...

Was this page helpful?
0 / 5 - 0 ratings