From 9578b72915305a0b0a2ceae1f0e6bb370b8da617 Mon Sep 17 00:00:00 2001 From: Owen Smith Date: Fri, 16 Oct 2026 15:28:04 -0400 Subject: [PATCH] ConstantAttributeAnalzyer: fix an ArgumentNullException from throw expressions --- src/D2L.CodeStyle.Analyzers/D2L.CodeStyle.Analyzers.csproj | 2 +- .../D2L.CodeStyle.TestAnalyzers.csproj | 2 +- .../ConstantAttributeAnalyzer.cs | 4 ++++ .../Specs/ConstantAttributeAnalyzer.cs | 1 + 4 files changed, 7 insertions(+), 2 deletions(-) diff --git a/src/D2L.CodeStyle.Analyzers/D2L.CodeStyle.Analyzers.csproj b/src/D2L.CodeStyle.Analyzers/D2L.CodeStyle.Analyzers.csproj index a2962efb..471366c3 100644 --- a/src/D2L.CodeStyle.Analyzers/D2L.CodeStyle.Analyzers.csproj +++ b/src/D2L.CodeStyle.Analyzers/D2L.CodeStyle.Analyzers.csproj @@ -6,7 +6,7 @@ D2L.CodeStyle.Analyzers D2L.CodeStyle D2L.CodeStyle analyzers - 0.230.0 + 0.231.0 Apache-2.0 https://github.com/Brightspace/D2L.CodeStyle D2L diff --git a/src/D2L.CodeStyle.TestAnalyzers/D2L.CodeStyle.TestAnalyzers.csproj b/src/D2L.CodeStyle.TestAnalyzers/D2L.CodeStyle.TestAnalyzers.csproj index f94b35e1..94d9d9c4 100644 --- a/src/D2L.CodeStyle.TestAnalyzers/D2L.CodeStyle.TestAnalyzers.csproj +++ b/src/D2L.CodeStyle.TestAnalyzers/D2L.CodeStyle.TestAnalyzers.csproj @@ -6,7 +6,7 @@ D2L.CodeStyle.TestAnalyzers D2L.CodeStyle D2L.CodeStyle test analyzers - 0.33.0 + 0.34.0 Apache-2.0 https://github.com/Brightspace/D2L.CodeStyle D2L diff --git a/src/Rules/D2L.CodeStyle.Analyzers.Rule.Constant/D2L.CodeStyle.Analyzers.Rule.Constant/ConstantAttributeAnalyzer.cs b/src/Rules/D2L.CodeStyle.Analyzers.Rule.Constant/D2L.CodeStyle.Analyzers.Rule.Constant/ConstantAttributeAnalyzer.cs index b5b4a97a..3f3db4c5 100644 --- a/src/Rules/D2L.CodeStyle.Analyzers.Rule.Constant/D2L.CodeStyle.Analyzers.Rule.Constant/ConstantAttributeAnalyzer.cs +++ b/src/Rules/D2L.CodeStyle.Analyzers.Rule.Constant/D2L.CodeStyle.Analyzers.Rule.Constant/ConstantAttributeAnalyzer.cs @@ -190,6 +190,10 @@ private static void AnalyzeConversion( ITypeSymbol targetType, SyntaxNode diagnosticSyntax ) { + if( sourceType is null ) { + return; + } + Conversion conversion = context.Compilation.ClassifyConversion( source: sourceType, destination: targetType diff --git a/tests/D2L.CodeStyle.Analyzers.Test/Specs/ConstantAttributeAnalyzer.cs b/tests/D2L.CodeStyle.Analyzers.Test/Specs/ConstantAttributeAnalyzer.cs index 72b31270..c5dfa637 100644 --- a/tests/D2L.CodeStyle.Analyzers.Test/Specs/ConstantAttributeAnalyzer.cs +++ b/tests/D2L.CodeStyle.Analyzers.Test/Specs/ConstantAttributeAnalyzer.cs @@ -275,6 +275,7 @@ string untrusted { Types.ConstantStruct v = trusted; } { Types.ConstantStruct v = /* NonConstantPassedToConstantParameter(value) */ variable /**/; } { Types.ConstantStruct v = /* NonConstantPassedToConstantParameter(value) */ untrusted /**/; } + { Types.ConstantStruct v = /* NonConstantPassedToConstantParameter(value) */ trusted ?? throw new ArgumentNullException( nameof( trusted ) ) /**/; } { (Types.ConstantStruct X, Types.ConstantStruct Y) v = (Constants.String, Constants.String); } { (Types.ConstantStruct X, Types.ConstantStruct Y) v = (Constants.String, trusted); } { (Types.ConstantStruct X, Types.ConstantStruct Y) v = (Constants.String, /* NonConstantPassedToConstantParameter(value) */ variable /**/); }