Skip to content
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

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
<?xml version="1.0" encoding="utf-8" ?>
<diagnostics severity="Hidden">
<languageVersion>9.0</languageVersion>
<suppressId>CS8799</suppressId>
<diagnostic id="WTG1001" message="Our convention is to omit the 'private' modifier where it is already the default.">
<location>Test0.cs: (15,3-10)</location>
<location>Test0.cs: (9,3-10)</location>
</diagnostic>
<diagnostic id="WTG1001" message="Our convention is to omit the 'private' modifier where it is already the default.">
<location>Test0.cs: (19,3-10)</location>
</diagnostic>
<diagnostic id="WTG1001" message="Our convention is to omit the 'private' modifier where it is already the default.">
<location>Test0.cs: (25,3-10)</location>
</diagnostic>
<diagnostic id="WTG1006" message="Our convention is to omit the 'internal' modifier on types where it is already the default." >
<location>Test0.cs: (20,2-10)</location>
<location>Test0.cs: (30,2-10)</location>
</diagnostic>
</diagnostics>
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,21 @@ namespace ns
partial class Foo
{
private partial int Bar();
public partial void FooBar();
private partial int FooBarBaz(out int value);

partial void Qux();
private partial void Quux(out int value);
}

partial class Foo
{
private partial int Bar() { return default; }
public partial void FooBar() { }
private partial int FooBarBaz(out int value) => throw null;

partial void Qux() { }
private partial void Quux(out int value) { value = default; }
}

class Outer
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,21 @@ namespace ns
partial class Foo
{
private partial int Bar();
public partial void FooBar();
private partial int FooBarBaz(out int value);

private partial void Qux();
private partial void Quux(out int value);
}

partial class Foo
{
private partial int Bar() { return default; }
public partial void FooBar() { }
private partial int FooBarBaz(out int value) => throw null;

private partial void Qux() { }
private partial void Quux(out int value) { value = default; }
}

class Outer
Expand Down
35 changes: 31 additions & 4 deletions src/WTG.Analyzers/Analyzers/Visibility/VisibilityAnalyzer.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using System.Collections.Immutable;
using System.Linq;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Diagnostics;
using WTG.Analyzers.Utils;

Expand Down Expand Up @@ -32,7 +34,8 @@ static void Analyze(SyntaxNodeAnalysisContext context, FileDetailCache cache)
return;
}

var list = ModifierExtractionVisitor.Instance.Visit(context.Node);
var currentNode = context.Node;
var list = ModifierExtractionVisitor.Instance.Visit(currentNode);
var privateToken = default(SyntaxToken);

foreach (var modifier in list)
Expand All @@ -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 (PartialMethodRequiresAccessibilityModifier(currentNode)):
return;

case SyntaxKind.InternalKeyword:
if (IsTopLevel(context.Node))
if (IsTopLevel(currentNode))
{
context.ReportDiagnostic(Rules.CreateDoNotUseTheInternalKeywordForTopLevelTypesDiagnostic(modifier.GetLocation()));
}
Expand All @@ -65,6 +67,31 @@ static void Analyze(SyntaxNodeAnalysisContext context, FileDetailCache cache)
}
}

static bool PartialMethodRequiresAccessibilityModifier(SyntaxNode node)
{
if (!node.IsKind(SyntaxKind.MethodDeclaration))
{
return false;
}

var methodNode = (MethodDeclarationSyntax)node;
if (methodNode.ReturnType.IsKind(SyntaxKind.PredefinedType))
{
var returnType = (PredefinedTypeSyntax)methodNode.ReturnType;
if (!returnType.Keyword.IsKind(SyntaxKind.VoidKeyword))
{
return true;
}
}

if (methodNode.ParameterList?.Parameters.Any(static p => p.Modifiers.Any(SyntaxKind.OutKeyword)) == true)
{
return true;
}

return false;
}

static bool IsTopLevel(SyntaxNode node)
{
var parentKind = node.Parent?.Kind() ?? SyntaxKind.None;
Expand Down
Loading