roslyn icon indicating copy to clipboard operation
roslyn copied to clipboard

False Positive with IDE0370

Open Evangelink opened this issue 1 month ago • 8 comments

Version Used: VS Insiders [11304.161]

Steps to Reproduce: Open testf solution.

A minimal repro, with source-code provided, is ideal. Most compiler/language issues can be distilled into a snippet that can be pasted into .NET Lab.

Diagnostic Id: IDE0370 Suppression is unecessary

Expected Behavior:

No IDE0370 on the snippet below because GetMethod returns nullable.

Actual Behavior:

public void HasCorrectClassOrAssemblyInitializeSignatureShouldReturnTrueForTestMethods()
{
    MethodInfo methodInfo = typeof(DummyTestClass).GetMethod("PublicStaticMethodWithTC")!; // IDE0370 here
    methodInfo.HasCorrectClassOrAssemblyInitializeSignature().Should().BeTrue();
}

Evangelink avatar Dec 10 '25 08:12 Evangelink

Looks like tehse are reporting in net462/net48, which is appropriate. Those suppressions are not necessary there.

Unfortunately, a fundamental restriction of analyzers is they run per-compilation. So they don't know about other 'flavors' that you are compiling for where the suppression may be necessary. The fixer does know though, and won't remove these if you do 'fix all in solution'.

Given htat you are targeting old runtimes, you probably just want to disable this analyzer.

CyrusNajmabadi avatar Dec 10 '25 11:12 CyrusNajmabadi

In netfx, is it really correct that the compiler assumes it's non-nullable? I feel like when the nullability info are not present, compiler should always assume null. Am I missing something?

Evangelink avatar Dec 10 '25 12:12 Evangelink

The analyzer is saying that the supression is not necessary and can eb safely removed. That is true in netfx. just as there is no nullability info, the suppression is also not needed.

Imagine if we didn't have this. someone might have a method with all sorts of unneccessary ! in there that they thought they needed. and they'd never know they coudl remove it.

maybe there is some wya to disable that analyzer jsut for that particular compilation flavor.

CyrusNajmabadi avatar Dec 10 '25 13:12 CyrusNajmabadi

maybe there is some wya to disable that analyzer jsut for that particular compilation flavor.

I was going to ask if you know a trick. Because with .editorconfig I don't know how I can disable this for netfx only.

Evangelink avatar Dec 10 '25 13:12 Evangelink

@jaredpar is there a way to disable analyzers or provide a specific editorconfig/options set to a particular analysis of one flavor of a project?

CyrusNajmabadi avatar Dec 10 '25 14:12 CyrusNajmabadi

There is no way to toggle editorconfig based on TFM, at least that I'm aware of. When we've hit problems like this in the past we've just used the <NoWarn Condition="'$(TargetFramework)' == 'net472'>... approach

jaredpar avatar Dec 10 '25 16:12 jaredpar

There is also no way to conditionally include .editorconfig files based on the TFM, right? Only globalconfig files can be included/excluded in that way, I think.

RikkiGibson avatar Dec 10 '25 19:12 RikkiGibson

Can we combine ruleset and editorconfig? Maybe that could be a way.

Although the NoWarn is not bad.

Evangelink avatar Dec 10 '25 19:12 Evangelink