-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add analyzer for object parameter (#243)
- Loading branch information
Showing
5 changed files
with
161 additions
and
0 deletions.
There are no files selected for viewing
80 changes: 80 additions & 0 deletions
80
...Dhgms.GripeWithRoslyn.Analyzer/Analyzers/Runtime/DoNotUseObjectAsParameterTypeAnalyzer.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
// Copyright (c) 2019 DHGMS Solutions and Contributors. All rights reserved. | ||
// This file is licensed to you under the MIT license. | ||
// See the LICENSE file in the project root for full license information. | ||
|
||
using System.Collections.Immutable; | ||
using Dhgms.GripeWithRoslyn.Analyzer.CodeCracker.Extensions; | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.CSharp; | ||
using Microsoft.CodeAnalysis.CSharp.Syntax; | ||
using Microsoft.CodeAnalysis.Diagnostics; | ||
|
||
namespace Dhgms.GripeWithRoslyn.Analyzer.Analyzers.Runtime | ||
{ | ||
/// <summary> | ||
/// Analyzer to ensure <see cref="object"/> is not used in a parameter declaration. | ||
/// </summary> | ||
[DiagnosticAnalyzer(LanguageNames.CSharp)] | ||
public sealed class DoNotUseObjectAsParameterTypeAnalyzer : DiagnosticAnalyzer | ||
{ | ||
internal const string Title = "Do not use Object in a parameter declaration."; | ||
|
||
private const string MessageFormat = Title; | ||
|
||
private const string Category = SupportedCategories.Design; | ||
|
||
private readonly DiagnosticDescriptor _rule; | ||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="DoNotUseObjectAsParameterTypeAnalyzer"/> class. | ||
/// </summary> | ||
public DoNotUseObjectAsParameterTypeAnalyzer() | ||
{ | ||
_rule = new DiagnosticDescriptor( | ||
DiagnosticIdsHelper.DoNotUseObjectAsParameterType, | ||
Title, | ||
MessageFormat, | ||
Category, | ||
DiagnosticSeverity.Warning, | ||
isEnabledByDefault: true, | ||
description: DiagnosticResultDescriptionFactory.DoNotUseObjectAsParameterType()); | ||
} | ||
|
||
/// <inheritdoc /> | ||
public override ImmutableArray<DiagnosticDescriptor> SupportedDiagnostics => ImmutableArray.Create(_rule); | ||
|
||
/// <inheritdoc /> | ||
public sealed override void Initialize(AnalysisContext context) | ||
{ | ||
context.EnableConcurrentExecution(); | ||
context.ConfigureGeneratedCodeAnalysis(GeneratedCodeAnalysisFlags.Analyze | GeneratedCodeAnalysisFlags.ReportDiagnostics); | ||
context.RegisterSyntaxNodeAction(AnalyzeParameter, SyntaxKind.Parameter); | ||
} | ||
|
||
private void AnalyzeParameter(SyntaxNodeAnalysisContext syntaxNodeAnalysisContext) | ||
{ | ||
var parameterSyntax = (ParameterSyntax)syntaxNodeAnalysisContext.Node; | ||
|
||
var semanticModel = syntaxNodeAnalysisContext.SemanticModel; | ||
var type = parameterSyntax.Type; | ||
if (type == null) | ||
{ | ||
return; | ||
} | ||
|
||
var baseTypeInfo = semanticModel.GetTypeInfo(type); | ||
var baseTypeSymbol = baseTypeInfo.Type; | ||
|
||
if (baseTypeSymbol == null) | ||
{ | ||
return; | ||
} | ||
|
||
var fullName = baseTypeSymbol.GetFullName(true); | ||
if (fullName == "global::System.Object") | ||
{ | ||
syntaxNodeAnalysisContext.ReportDiagnostic(Diagnostic.Create(_rule, type.GetLocation())); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 76 additions & 0 deletions
76
...Roslyn.UnitTests/Analyzer/Analyzers/Runtime/DoNotUseObjectAsParameterTypeAnalyzerTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
// Copyright (c) 2019 DHGMS Solutions and Contributors. All rights reserved. | ||
// This file is licensed to you under the MIT license. | ||
// See the LICENSE file in the project root for full license information. | ||
|
||
using Dhgms.GripeWithRoslyn.Analyzer; | ||
using Dhgms.GripeWithRoslyn.Analyzer.Analyzers.Runtime; | ||
using Dhgms.GripeWithRoslyn.UnitTests.Helpers; | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.Diagnostics; | ||
using Xunit; | ||
|
||
namespace Dhgms.GripeWithRoslyn.UnitTests.Analyzer.Analyzers.Runtime | ||
{ | ||
/// <summary> | ||
/// Unit Tests for <see cref="DoNotUseObjectAsParameterTypeAnalyzer"/>. | ||
/// </summary> | ||
public sealed class DoNotUseObjectAsParameterTypeAnalyzerTests : CodeFixVerifier | ||
{ | ||
/// <summary> | ||
/// Test to ensure bad code returns a warning. | ||
/// </summary> | ||
[Fact] | ||
public void ReturnsWarning() | ||
{ | ||
var test = @" | ||
namespace ConsoleApplication1 | ||
{ | ||
class TypeName | ||
{ | ||
public void MethodName(object arg) | ||
{ | ||
var name = SomeEnum.SomeValue.ToString(); | ||
} | ||
public void MethodName2(System.Object arg) | ||
{ | ||
var name = SomeEnum.SomeValue.ToString(); | ||
} | ||
} | ||
}"; | ||
var expected = new[] | ||
{ | ||
new DiagnosticResult | ||
{ | ||
Id = DiagnosticIdsHelper.DoNotUseObjectAsParameterType, | ||
Message = DoNotUseObjectAsParameterTypeAnalyzer.Title, | ||
Severity = DiagnosticSeverity.Warning, | ||
Locations = | ||
[ | ||
new DiagnosticResultLocation("Test0.cs", 6, 36) | ||
] | ||
}, | ||
new DiagnosticResult | ||
{ | ||
Id = DiagnosticIdsHelper.DoNotUseObjectAsParameterType, | ||
Message = DoNotUseObjectAsParameterTypeAnalyzer.Title, | ||
Severity = DiagnosticSeverity.Warning, | ||
Locations = | ||
[ | ||
new DiagnosticResultLocation("Test0.cs", 11, 37) | ||
] | ||
} | ||
}; | ||
|
||
VerifyCSharpDiagnostic( | ||
test, | ||
expected); | ||
} | ||
|
||
/// <inheritdoc /> | ||
protected override DiagnosticAnalyzer GetCSharpDiagnosticAnalyzer() | ||
{ | ||
return new DoNotUseObjectAsParameterTypeAnalyzer(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters