Skip to content

Commit e0bd5dd

Browse files
committed
- add more cases
- 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
1 parent 0004a38 commit e0bd5dd

File tree

4 files changed

+41
-6
lines changed

4 files changed

+41
-6
lines changed
Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,17 @@
11
<?xml version="1.0" encoding="utf-8" ?>
22
<diagnostics severity="Hidden">
33
<languageVersion>9.0</languageVersion>
4+
<suppressId justtification="To ignore when only one method in one class have the code fix removing 'private' applied.">CS8799</suppressId>
45
<diagnostic id="WTG1001" message="Our convention is to omit the 'private' modifier where it is already the default.">
5-
<location>Test0.cs: (15,3-10)</location>
6+
<location>Test0.cs: (9,3-10)</location>
7+
</diagnostic>
8+
<diagnostic id="WTG1001" message="Our convention is to omit the 'private' modifier where it is already the default.">
9+
<location>Test0.cs: (19,3-10)</location>
10+
</diagnostic>
11+
<diagnostic id="WTG1001" message="Our convention is to omit the 'private' modifier where it is already the default.">
12+
<location>Test0.cs: (25,3-10)</location>
613
</diagnostic>
714
<diagnostic id="WTG1006" message="Our convention is to omit the 'internal' modifier on types where it is already the default." >
8-
<location>Test0.cs: (20,2-10)</location>
15+
<location>Test0.cs: (30,2-10)</location>
916
</diagnostic>
1017
</diagnostics>

src/WTG.Analyzers.Test/TestData/VisibilityAnalyzer/Partial/Result.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ partial class Foo
44
{
55
private partial int Bar();
66
public partial void FooBar();
7+
private partial int FooBarBaz(out int value);
78

89
partial void Qux();
910
private partial void Quux(out int value);
@@ -13,6 +14,7 @@ partial class Foo
1314
{
1415
private partial int Bar() { return default; }
1516
public partial void FooBar() { }
17+
private partial int FooBarBaz(out int value) { value = default; return default; }
1618

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

src/WTG.Analyzers.Test/TestData/VisibilityAnalyzer/Partial/Source.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ partial class Foo
44
{
55
private partial int Bar();
66
public partial void FooBar();
7+
private partial int FooBarBaz(out int value);
78

89
private partial void Qux();
910
private partial void Quux(out int value);
@@ -13,6 +14,7 @@ partial class Foo
1314
{
1415
private partial int Bar() { return default; }
1516
public partial void FooBar() { }
17+
private partial int FooBarBaz(out int value) { value = default; return default; }
1618

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

src/WTG.Analyzers/Analyzers/Visibility/VisibilityAnalyzer.cs

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
using System.Collections.Immutable;
2+
using System.Linq;
23
using Microsoft.CodeAnalysis;
34
using Microsoft.CodeAnalysis.CSharp;
5+
using Microsoft.CodeAnalysis.CSharp.Syntax;
46
using Microsoft.CodeAnalysis.Diagnostics;
57
using WTG.Analyzers.Utils;
68

@@ -32,7 +34,8 @@ static void Analyze(SyntaxNodeAnalysisContext context, FileDetailCache cache)
3234
return;
3335
}
3436

35-
var list = ModifierExtractionVisitor.Instance.Visit(context.Node);
37+
var currentNode = context.Node;
38+
var list = ModifierExtractionVisitor.Instance.Visit(currentNode);
3639
var privateToken = default(SyntaxToken);
3740

3841
foreach (var modifier in list)
@@ -47,11 +50,10 @@ static void Analyze(SyntaxNodeAnalysisContext context, FileDetailCache cache)
4750

4851
case SyntaxKind.ProtectedKeyword:
4952
case SyntaxKind.PublicKeyword:
50-
case SyntaxKind.PartialKeyword when (context.Node.IsKind(SyntaxKind.MethodDeclaration)):
53+
case SyntaxKind.PartialKeyword when (DoesMethodHaveReturnValueOrOutKeyword(currentNode)):
5154
return;
52-
5355
case SyntaxKind.InternalKeyword:
54-
if (IsTopLevel(context.Node))
56+
if (IsTopLevel(currentNode))
5557
{
5658
context.ReportDiagnostic(Rules.CreateDoNotUseTheInternalKeywordForTopLevelTypesDiagnostic(modifier.GetLocation()));
5759
}
@@ -65,6 +67,28 @@ static void Analyze(SyntaxNodeAnalysisContext context, FileDetailCache cache)
6567
}
6668
}
6769

70+
static bool DoesMethodHaveReturnValueOrOutKeyword(SyntaxNode node)
71+
{
72+
if (!node.IsKind(SyntaxKind.MethodDeclaration))
73+
{
74+
return false;
75+
}
76+
77+
var methodNode = (MethodDeclarationSyntax)node;
78+
if (methodNode.ReturnType is PredefinedTypeSyntax predefinedTypeSyntax
79+
&& !predefinedTypeSyntax.Keyword.IsKind(SyntaxKind.VoidKeyword))
80+
{
81+
return true;
82+
}
83+
84+
if (methodNode.ParameterList?.Parameters.Any(c => c.Modifiers.Any(SyntaxKind.OutKeyword)) == true)
85+
{
86+
return true;
87+
}
88+
89+
return false;
90+
}
91+
6892
static bool IsTopLevel(SyntaxNode node)
6993
{
7094
var parentKind = node.Parent?.Kind() ?? SyntaxKind.None;

0 commit comments

Comments
 (0)