Added detection for compile-time constant patterns

This commit is contained in:
Thorsten Sommer 2026-06-09 10:28:04 +02:00
parent e9da7d31df
commit d91b11e32a
Signed by untrusted user who does not match committer: tsommer
GPG Key ID: 371BBA77A02C0108

View File

@ -22,7 +22,7 @@ public sealed class EmptyStringAnalyzer : DiagnosticAnalyzer
Use string.Empty instead of "" Use string.Empty instead of ""
"""; """;
private static readonly string DESCRIPTION = """Empty string literals ("") should be replaced with string.Empty for better code consistency and readability except in const contexts."""; private static readonly string DESCRIPTION = """Empty string literals ("") should be replaced with string.Empty for better code consistency and readability except in contexts requiring compile-time constants.""";
private const string CATEGORY = "Usage"; private const string CATEGORY = "Usage";
@ -43,20 +43,26 @@ public sealed class EmptyStringAnalyzer : DiagnosticAnalyzer
if (stringLiteral.Token.ValueText != string.Empty) if (stringLiteral.Token.ValueText != string.Empty)
return; return;
if (IsInConstContext(stringLiteral)) if (RequiresCompileTimeConstant(stringLiteral))
return;
if (IsInParameterDefaultValue(stringLiteral))
return; return;
var diagnostic = Diagnostic.Create(RULE, stringLiteral.GetLocation()); var diagnostic = Diagnostic.Create(RULE, stringLiteral.GetLocation());
context.ReportDiagnostic(diagnostic); context.ReportDiagnostic(diagnostic);
} }
private static bool IsInConstContext(LiteralExpressionSyntax stringLiteral) private static bool RequiresCompileTimeConstant(LiteralExpressionSyntax stringLiteral)
{
return IsInConstDeclarationInitializer(stringLiteral)
|| IsInParameterDefaultValue(stringLiteral)
|| IsInAttributeArgument(stringLiteral)
|| IsInSwitchCaseLabel(stringLiteral)
|| IsInConstantPattern(stringLiteral);
}
private static bool IsInConstDeclarationInitializer(LiteralExpressionSyntax stringLiteral)
{ {
var variableDeclarator = stringLiteral.FirstAncestorOrSelf<VariableDeclaratorSyntax>(); var variableDeclarator = stringLiteral.FirstAncestorOrSelf<VariableDeclaratorSyntax>();
if (variableDeclarator is null) if (variableDeclarator?.Initializer is null || !ContainsNode(variableDeclarator.Initializer.Value, stringLiteral))
return false; return false;
var declaration = variableDeclarator.Parent?.Parent; var declaration = variableDeclarator.Parent?.Parent;
@ -71,18 +77,30 @@ public sealed class EmptyStringAnalyzer : DiagnosticAnalyzer
private static bool IsInParameterDefaultValue(LiteralExpressionSyntax stringLiteral) private static bool IsInParameterDefaultValue(LiteralExpressionSyntax stringLiteral)
{ {
// Prüfen, ob das String-Literal Teil eines Parameter-Defaults ist
var parameter = stringLiteral.FirstAncestorOrSelf<ParameterSyntax>(); var parameter = stringLiteral.FirstAncestorOrSelf<ParameterSyntax>();
if (parameter is null) return parameter?.Default is not null && ContainsNode(parameter.Default.Value, stringLiteral);
return false; }
// Überprüfen, ob das String-Literal im Default-Wert des Parameters verwendet wird private static bool IsInAttributeArgument(LiteralExpressionSyntax stringLiteral)
if (parameter.Default is not null && {
parameter.Default.Value == stringLiteral) var attributeArgument = stringLiteral.FirstAncestorOrSelf<AttributeArgumentSyntax>();
{ return attributeArgument is not null && ContainsNode(attributeArgument.Expression, stringLiteral);
return true; }
}
return false; private static bool IsInSwitchCaseLabel(LiteralExpressionSyntax stringLiteral)
{
var caseSwitchLabel = stringLiteral.FirstAncestorOrSelf<CaseSwitchLabelSyntax>();
return caseSwitchLabel is not null && ContainsNode(caseSwitchLabel.Value, stringLiteral);
}
private static bool IsInConstantPattern(LiteralExpressionSyntax stringLiteral)
{
var constantPattern = stringLiteral.FirstAncestorOrSelf<ConstantPatternSyntax>();
return constantPattern is not null && ContainsNode(constantPattern.Expression, stringLiteral);
}
private static bool ContainsNode(SyntaxNode parent, SyntaxNode child)
{
return parent.SpanStart <= child.SpanStart && child.Span.End <= parent.Span.End;
} }
} }