Closed andrewhongnsw closed 1 year ago
My suggestion is something like this in PointlessOverrideAnalyzer.cs
:
if (IsOverride(node) && !DeclarationHasKeyword(node, SyntaxKind.SealedKeyword) && !node.Accept(IsMeaningfulVisitor.Instance))
and
static bool DeclarationHasKeyword(CSharpSyntaxNode node, SyntaxKind keyword)
{
return node switch
{
MethodDeclarationSyntax => ((MethodDeclarationSyntax)node).Modifiers.Any(x => x.IsKind(keyword)),
PropertyDeclarationSyntax => ((PropertyDeclarationSyntax)node).Modifiers.Any(x => x.IsKind(keyword)),
IndexerDeclarationSyntax => ((IndexerDeclarationSyntax)node).Modifiers.Any(x => x.IsKind(keyword)),
EventDeclarationSyntax => ((EventDeclarationSyntax)node).Modifiers.Any(x => x.IsKind(keyword)),
_ => false,
};
}
I agree that it should not be considered pointless if it's sealed, though I wouldn't implement the check like that. I would suggest using an approach similar to what IsOverride
does; in fact, it might be a good idea to merge the two.
bool IsApplicable(CSharpSyntaxNode node)
{
var modifiers = node.Accept(ModifierExtractionVisitor.Instance);
return HasToken(modifiers, SyntaxKind.OverrideKeyword) && !HasToken(modifiers, SyntaxKind.Sealed);
}
static bool HasToken(SyntaxTokenList list, SyntaxKind kind)
{
foreach (var token in list)
{
if (token.IsKind(kind))
{
return true;
}
}
return false;
}
or perhaps
bool IsApplicable(CSharpSyntaxNode node)
{
var modifiers = node.Accept(ModifierExtractionVisitor.Instance);
var hasOverride = false;
foreach (var token in modifiers)
{
switch (token.Kind())
{
case SyntaxKind.SealedKeyword:
case SyntaxKind.VirtualKeyword:
return false;
case SyntaxKind.OverrideKeyword:
hasOverride = true;
break;
}
}
return hasOverride;
}
Created WI00577893 to track this internally.
I had already created WI00577538 :)
lol ok, cancelled WI00577893
In the following code,
WTG3007
triggers at each of the four overrides - however sealing is arguably not pointless as it prevents further overrides.Recommend checking for the
sealed
keyword and allowing overrides in those cases.