S1128 false-positive when obsolete attribute is used

  • What language is this for? C#
  • Which rule? S1128
  • Why do you believe it’s a false-positive/false-negative? false-positive
  • Are you using
    • SonarQube Cloud? No
    • SonarQube Server / Community Build - which version? v25.1.0.102122
    • SonarQube for IDE - which IDE/version? VS 2022
      • in connected mode with SonarQube Server / Community Build or SonarQube Cloud? SonarQube Community Build
  • How can we reproduce the problem? Give us a self-contained snippet of code (formatted text, no screenshots)

Obsolete attribute requires ‘using System;’ but it is marked as unnecessary in the analysis

using System; // S1128 raised here

namespace TestNameSpace
{
    public class TestClass
    {
        #if !NET48
            [Obsolete("Obsolete message")] // requires using System;
        #endif
        public IEnumerable<T> GetList<T>()
        {
            // implementation
        }
    }
}

I think the title is misleading; it is not about the [Obsolete] attribute but the #if pragma. That aside it is a FP indeed.

Hello @haraman21,

Welcome to the community!

I was not able to reproduce the issue locally.

Where is the issue showing up? Is it in your IDE (through SonarQube IDE) or in your SonarQube?
Can you tell me which version of your SonarQube IDE and the version of the C# analyzer you are using?
You can find the C# analyzer version in the logs when running the begin step of the SonarScanner for .NET. It should look like something like this:

Processing plugin: csharp version X.X.X.XXXX

If you are able to give me the source (including the csproj) of a very small project that reproduce the issue, it would be really helpful :pray:

Thank you,

Note that this one can easily be solved:

namespace TestNameSpace
{
    public class TestClass
    {
        #if !NET48
            [System.Obsolete("Obsolete message")]
        #endif
        public IEnumerable<T> GetList<T>()
        {
            // implementation
        }
    }
}

(or by globally using System)

Hi
Its VS 2022 17.12.3
sonarqube-25.2.0.102705 (now updated-from 25.1.2.102122)
SonarQube IDE 8.11.0.11944
csharp version 10.6.0.109712 (now updated-from 10.4.0.108396)
sonar-scanner-9.1.0.109947-net-framework (now updated-from 9.0.2.104486)

Its a library that multi-targets - net48 and netstandard2.0
Nothing specifically configured, csproj with all default settings except netstandard2.0;net48

Can not use global usings, projects targets .net fx 4.8, so stuck with c#7.3

System.Obolete will suppress the prompt but doesn’t it defeat the purpose of using ... directive? The ability to use types defined in a namespace without specifying the fully qualified namespace of that type.

Sure it does. The rue raises a FP, but - as mentioned - the Roslyn context makes at nontrivial to solve this one. And you have apparently have one place where you use something from the System namespace, so I would go for it while this is not fixed.