Using SuppressMessage on a const declaration causes Diagnostic analyzer runner interal error
- Dominant language
- C#
- Stars
- 20.7k
- Forks
- 4.3k
- PR merge metrics
- PR metrics pending
Description
_This issue has been moved from [a ticket on Developer Community](https://developercommunity.visualstudio.com/t/Using-SupressMessage-on-a-const-declarat/10723748)._
---
[severity:It bothers me. A fix would be nice]
Add the following const to a C# file will cause internal error in Diagnostic analyzer runner. Exception stack posted following code sample.
```
public class Win32PrintDialog
{
///
/// Win32 PRINTDLG/PRINTDLGEX constants.
///
[SuppressMessage("ReSharper", "InconsistentNaming")]
[SuppressMessage("ReSharper", "IdentifierTypo")]
[SuppressMessage("ReSharper", "UnusedMember.Global")]
protected const int
PD_ALLPAGES = 0x00000000,
PD_SELECTION = 0x00000001,
PD_PAGENUMS = 0x00000002,
PD_NOSELECTION = 0x00000004,
PD_NOPAGENUMS = 0x00000008,
PD_COLLATE = 0x00000010,
PD_PRINTTOFILE = 0x00000020,
PD_PRINTSETUP = 0x00000040,
PD_NOWARNING = 0x00000080,
PD_RETURNDC = 0x00000100,
PD_RETURNIC = 0x00000200,
PD_RETURNDEFAULT = 0x00000400,
PD_SHOWHELP = 0x00000800,
PD_ENABLEPRINTHOOK = 0x00001000,
PD_ENABLESETUPHOOK = 0x00002000,
PD_ENABLEPRINTTEMPLATE = 0x00004000,
PD_ENABLESETUPTEMPLATE = 0x00008000,
PD_ENABLEPRINTTEMPLATEHANDLE = 0x00010000,
PD_ENABLESETUPTEMPLATEHANDLE = 0x00020000,
PD_USEDEVMODECOPIES = 0x00040000,
PD_USEDEVMODECOPIESANDCOLLATE = 0x00040000,
PD_DISABLEPRINTTOFILE = 0x00080000,
PD_HIDEPRINTTOFILE = 0x00100000,
PD_NONETWORKBUTTON = 0x00200000,
PD_CURRENTPAGE = 0x00400000,
PD_NOCURRENTPAGE = 0x00800000,
PD_EXCLUSIONFLAGS = 0x01000000,
PD_USELARGETEMPLATE = 0x10000000,
PD_RESULT_CANCEL = 0,
PD_RESULT_PRINT = 1,
PD_RESULT_APPLY = 2;
}
```
```
StreamJsonRpc.RemoteInvocationException: An item with the same key has already been added. Key: SuppressMessage("ReSharper", "InconsistentNaming")
at StreamJsonRpc.JsonRpc. d__154`1.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.CodeAnalysis.Remote.BrokeredServiceConnection`1. d__18`1.MoveNext()
RPC server exception:
System.ArgumentException: An item with the same key has already been added. Key: SuppressMessage("ReSharper", "InconsistentNaming")
at System.Collections.Generic.Dictionary`2.TryInsert(TKey key, TValue value, InsertionBehavior behavior)
at System.Collections.Generic.Dictionary`2.Add(TKey key, TValue value)
at Microsoft.CodeAnalysis.RemoveUnnecessarySuppressions.AbstractRemoveUnnecessaryInlineSuppressionsDiagnosticAnalyzer.ProcessSuppressMessageAttributesAsync(SyntaxNode root, SemanticModel semanticModel, Nullable`1 span, PooledDictionary`2 idToSuppressMessageAttributesMap, PooledDictionary`2 suppressMessageAttributesToIsUsedMap, ImmutableArray`1 userIdExclusions, ImmutableArray`1 userCategoryExclusions, CancellationToken cancellationToken)
at Microsoft.CodeAnalysis.RemoveUnnecessarySuppressions.AbstractRemoveUnnecessaryInlineSuppressionsDiagnosticAnalyzer.AnalyzeAsync(SemanticModel semanticModel, Nullable`1 span, CompilationWithAnalyzers compilationWithAnalyzers, Func`2 getSupportedDiagnostics, Action`1 reportDiagnostic, CancellationToken cancellationToken)
at Microsoft.CodeAnalysis.Diagnostics.Extensions.<>c__DisplayClass10_0. <g__AnalyzeDocumentAsync|0>d.MoveNext()
--- End of stack trace from previous location ---
at Microsoft.CodeAnalysis.Diagnostics.Extensions.GetPragmaSuppressionAnalyzerDiagnosticsAsync(CompilationWithAnalyzers compilationWithAnalyzers, DocumentAnalysisScope documentAnalysisScope, Project project, DiagnosticAnalyzerInfoCache analyzerInfoCache, CancellationToken cancellationToken)
at Microsoft.CodeAnalysis.Diagnostics.Extensions.GetAnalysisResultAsync(CompilationWithAnalyzers compilationWithAnalyzers, DocumentAnalysisScope documentAnalysisScope, Project project, DiagnosticAnalyzerInfoCache analyzerInfoCache, CancellationToken cancellationToken)
at Microsoft.CodeAnalysis.Remote.Diagnostics.DiagnosticComputer.AnalyzeAsync(CompilationWithAnalyzers compilationWithAnalyzers, BidirectionalMap`2 analyzerToIdMap, ImmutableArray`1 analyzers, SkippedHostAnalyzersInfo skippedAnalyzersInfo, Boolean reportSuppressedDiagnostics, Boolean logPerformanceInfo, Boolean getTelemetryInfo, CancellationToken cancellationToken)
at Microsoft.CodeAnalysis.Remote.Diagnostics.DiagnosticComputer.GetDiagnosticsAsync(IEnumerable`1 analyzerIds, Boolean reportSuppressedDiagnostics, Boolean logPerformanceInfo, Boolean getTelemetryInfo, CancellationToken cancellationToken)
at Microsoft.CodeAnalysis.Remote.Diagnostics.DiagnosticComputer.GetNormalPriorityDiagnosticsAsync(IEnumerable`1 analyzerIds, Boolean reportSuppressedDiagnostics, Boolean logPerformanceInfo, Boolean getTelemetryInfo, CancellationToken cancellationToken)
at Microsoft.CodeAnalysis.Remote.RemoteDiagnosticAnalyzerService.<>c__DisplayClass3_0. <b__0>d.MoveNext()
--- End of stack trace from previous location ---
at Microsoft.CodeAnalysis.Remote.RemoteWorkspace.<>c__DisplayClass8_0`1. <g__ProcessSolutionAsync|1>d.MoveNext()
--- End of stack trace from previous location ---
at Microsoft.CodeAnalysis.Remote.RemoteWorkspace.RunWithSolutionAsync[T](AssetProvider assetProvider, Checksum solutionChecksum, Int32 workspaceVersion, Boolean updatePrimaryBranch, Func`2 implementation, CancellationToken cancellationToken)
at Microsoft.CodeAnalysis.Remote.RemoteWorkspace.RunWithSolutionAsync[T](AssetProvider assetProvider, Checksum solutionChecksum, Int32 workspaceVersion, Boolean updatePrimaryBranch, Func`2 implementation, CancellationToken cancellationToken)
at Microsoft.CodeAnalysis.Remote.BrokeredServiceBase.RunWithSolutionAsync[T](Checksum solutionChecksum, Func`2 implementation, CancellationToken cancellationToken)
at Microsoft.CodeAnalysis.Remote.RemoteDiagnosticAnalyzerService.CalculateDiagnosticsAsync(Checksum solutionChecksum, DiagnosticArguments arguments, CancellationToken cancellationToken)
```
---
### Original Comments
(no comments)
---
### Original Solutions
(no solutions)
Contributor guide
Research direction
Reproduce the diagnostic analyzer failure with the C# const declaration and multiple SuppressMessage attributes shown in the issue. Start at AbstractRemoveUnnecessaryInlineSuppressionsDiagnosticAnalyzer.ProcessSuppressMessageAttributesAsync in the stack trace, then verify that the same input completes without the duplicate-key internal error.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- csharp
- Domain
- compilers, devtools
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 38/100