-
Notifications
You must be signed in to change notification settings - Fork 3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Cover WTG1001 on edge cases #225
Cover WTG1001 on edge cases #225
Conversation
- apply WTG1001 fix that removes 'private' keyword if the method is 'partial' and 'void' - keep 'private' keyword if a partial method is non-void or having 'out' keyword
@@ -47,11 +50,10 @@ static void Analyze(SyntaxNodeAnalysisContext context, FileDetailCache cache) | |||
|
|||
case SyntaxKind.ProtectedKeyword: | |||
case SyntaxKind.PublicKeyword: | |||
case SyntaxKind.PartialKeyword when (context.Node.IsKind(SyntaxKind.MethodDeclaration)): | |||
case SyntaxKind.PartialKeyword when (DoesMethodHaveReturnValueOrOutKeyword(currentNode)): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
case SyntaxKind.PartialKeyword when (DoesMethodHaveReturnValueOrOutKeyword(currentNode)): | |
case SyntaxKind.PartialKeyword when (PartialMethodRequiresAccessibilityModifier(currentNode)): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll apply the name as suggested.
} | ||
|
||
var methodNode = (MethodDeclarationSyntax)node; | ||
if (methodNode.ReturnType is PredefinedTypeSyntax predefinedTypeSyntax |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a Kind check we can perform on methodNode.ReturnType
before turning to runtime type casts?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I suppose the same as Kind check for var methodNode = (MethodDeclarationSyntax)node;
, e.g.
if (methodNode.ReturnType.IsKind(SyntaxKind.PredefinedType))
{
var returnType = (PredefinedTypeSyntax)methodNode.ReturnType;
if (!returnType.Keyword.IsKind(SyntaxKind.VoidKeyword))
{
return true;
}
}
return true; | ||
} | ||
|
||
if (methodNode.ParameterList?.Parameters.Any(c => c.Modifiers.Any(SyntaxKind.OutKeyword)) == true) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if (methodNode.ParameterList?.Parameters.Any(c => c.Modifiers.Any(SyntaxKind.OutKeyword)) == true) | |
if (methodNode.ParameterList?.Parameters.Any(static c => c.Modifiers.Any(SyntaxKind.OutKeyword)) == true) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll apply this.
src/WTG.Analyzers.Test/TestData/VisibilityAnalyzer/Partial/Diagnostics.xml
Outdated
Show resolved
Hide resolved
} | ||
|
||
partial class Foo | ||
{ | ||
private partial int Bar() { return default; } | ||
public partial void FooBar() { } | ||
private partial int FooBarBaz(out int value) { value = default; return default; } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Another option for methods that we don't care about:
private partial int FooBarBaz(out int value) { value = default; return default; } | |
private partial int FooBarBaz(out int value) => throw null; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll apply this.
Fix #224
Tracked by WI00764889
A method with default access modifier, partial, return value
e.g. private partial int Bar(); // no error as it returns value
A partial method with non-default access modifier, void
e.g. public partial void Bar(); // no error as it returns value
A partial method with default access modifier, void
e.g. private partial void Bar(); // trigger WTG1001 and remove 'private' when code-fix run
A partial method with default access modifier, void, out value
e.g. private partial void Bar(out int value); // no error as it 'out' a value
A partial method with default access modifier, return value, out value
e.g. private partial int Bar(out int value); // no error as it returns and 'out' a value