Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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 justtification="To ignore when only one method in one class have the code fix removing 'private' applied.">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) { value = default; return default; }

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) { value = default; return default; }
Copy link
Member

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:

Suggested change
private partial int FooBarBaz(out int value) { value = default; return default; }
private partial int FooBarBaz(out int value) => throw null;

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll apply this.


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

class Outer
Expand Down
32 changes: 28 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 (DoesMethodHaveReturnValueOrOutKeyword(currentNode)):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
case SyntaxKind.PartialKeyword when (DoesMethodHaveReturnValueOrOutKeyword(currentNode)):
case SyntaxKind.PartialKeyword when (PartialMethodRequiresAccessibilityModifier(currentNode)):

Copy link
Contributor Author

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.

return;

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

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

var methodNode = (MethodDeclarationSyntax)node;
if (methodNode.ReturnType is PredefinedTypeSyntax predefinedTypeSyntax
Copy link
Member

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?

Copy link
Contributor Author

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;
	}
}

&& !predefinedTypeSyntax.Keyword.IsKind(SyntaxKind.VoidKeyword))
{
return true;
}

if (methodNode.ParameterList?.Parameters.Any(c => c.Modifiers.Any(SyntaxKind.OutKeyword)) == true)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll apply this.

{
return true;
}

return false;
}

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