diff --git a/src/Rules/D2L.CodeStyle.Analyzers.Rule.PatternString/PatternStringAttributeAnalyzer.cs b/src/Rules/D2L.CodeStyle.Analyzers.Rule.PatternString/PatternStringAttributeAnalyzer.cs index 80f45863..c1439dc6 100644 --- a/src/Rules/D2L.CodeStyle.Analyzers.Rule.PatternString/PatternStringAttributeAnalyzer.cs +++ b/src/Rules/D2L.CodeStyle.Analyzers.Rule.PatternString/PatternStringAttributeAnalyzer.cs @@ -249,7 +249,7 @@ ConcurrentDictionary> regexCache // [PatternString] requires the assigned value to be a compile-time // constant so that we can evaluate it here. Optional constant = valueOperation.ConstantValue; - if( !constant.HasValue ) { + if( !constant.HasValue || constant.Value is null ) { context.ReportDiagnostic( Diagnostic.Create( descriptor: Diagnostics.PatternStringMustBeConstant, diff --git a/tests/D2L.CodeStyle.Analyzers.Test/Specs/PatternStringAttributeAnalyzer.cs b/tests/D2L.CodeStyle.Analyzers.Test/Specs/PatternStringAttributeAnalyzer.cs index c13ac2c5..c4cd295b 100644 --- a/tests/D2L.CodeStyle.Analyzers.Test/Specs/PatternStringAttributeAnalyzer.cs +++ b/tests/D2L.CodeStyle.Analyzers.Test/Specs/PatternStringAttributeAnalyzer.cs @@ -98,14 +98,19 @@ void SinglePatternTests() { } void NonConstantValuesAreFlagged() { - + #region Non-constant values are flagged as needing to be constant string variable = "123"; - #region Non-constant values are flagged as needing to be constant Digits _ = new( /* PatternStringMustBeConstant() */ variable /**/ ); Digits _ = /* PatternStringMustBeConstant() */ variable /**/; _ = (Digits)/* PatternStringMustBeConstant() */ variable /**/; #endregion + + #region null is treated as non-constant + Digits _ = new( /* PatternStringMustBeConstant() */ null /**/ ); + Digits _ = /* PatternStringMustBeConstant() */ null /**/; + _ = (Digits)/* PatternStringMustBeConstant() */ null /**/; + #endregion } void MultiplePatternTests() {