dotnet / roslyn

The Roslyn .NET compiler provides C# and Visual Basic languages with rich code analysis APIs.
https://docs.microsoft.com/dotnet/csharp/roslyn-sdk/
MIT License
18.97k stars 4.03k forks source link

SuppressMessage doesn't work on lambdas #59746

Open sbomer opened 2 years ago

sbomer commented 2 years ago

Version Used: 4.2.0-1.22116.16 (cb93a990)

Steps to Reproduce:

using System;
using System.Diagnostics.CodeAnalysis;

var lambda = [SuppressMessage("", "CA2013")] () => Object.ReferenceEquals(1, 2);

[SuppressMessage("", "CA2013")]
static void LocalFunction() => Object.ReferenceEquals(1, 2);

Expected Behavior:

SuppressMessage on the lambda silences warnings in the lambda, like it does for local functions.

Actual Behavior:

Program.cs(4,75): warning CA2013: Do not pass an argument with value type 'int' to 'ReferenceEquals'. Due to value boxing, this call to 'ReferenceEquals' will always return 'false'.
Program.cs(4,78): warning CA2013: Do not pass an argument with value type 'int' to 'ReferenceEquals'. Due to value boxing, this call to 'ReferenceEquals' will always return 'false'.

Same is true for UnconditionalSuppressMessage.

/cc @agocke @vitek-karas

Youssef1313 commented 2 years ago

Haven't debugged, so I might be wrong. But from a quick look:

https://github.com/dotnet/roslyn/blob/67a81ec3064e2886cf6faeed053e5220326cd5e1/src/Compilers/Core/Portable/DiagnosticAnalyzer/SuppressMessageAttributeState.cs#L166

It looks like this will be empty for lambdas per the implementation in:

https://github.com/dotnet/roslyn/blob/67a81ec3064e2886cf6faeed053e5220326cd5e1/src/Compilers/CSharp/Portable/Compilation/CSharpSemanticModel.cs#L5025-L5093

jcouv commented 2 years ago

Assigned to @cston. Putting in 17.2 since attributes on lambdas and local functions is relatively new feature.