-
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.
feature: Actionresult<TValue> check and more System.Object checks (#245)
- Loading branch information
Showing
12 changed files
with
887 additions
and
1 deletion.
There are no files selected for viewing
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
82 changes: 82 additions & 0 deletions
82
src/Dhgms.GripeWithRoslyn.Analyzer/Analyzers/Runtime/DoNotUseObjectAsFieldTypeAnalyzer.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,82 @@ | ||
// 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 Dhgms.GripeWithRoslyn.Analyzer.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 field declaration. | ||
/// </summary> | ||
[DiagnosticAnalyzer(LanguageNames.CSharp)] | ||
public sealed class DoNotUseObjectAsFieldTypeAnalyzer : DiagnosticAnalyzer | ||
{ | ||
internal const string Title = "Do not use Object in a field 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="DoNotUseObjectAsFieldTypeAnalyzer"/> class. | ||
/// </summary> | ||
public DoNotUseObjectAsFieldTypeAnalyzer() | ||
{ | ||
_rule = new DiagnosticDescriptor( | ||
DiagnosticIdsHelper.DoNotUseObjectAsFieldType, | ||
Title, | ||
MessageFormat, | ||
Category, | ||
DiagnosticSeverity.Warning, | ||
isEnabledByDefault: true, | ||
description: DiagnosticResultDescriptionFactory.DoNotUseObjectAsFieldType()); | ||
} | ||
|
||
/// <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.FieldDeclaration); | ||
} | ||
|
||
private void AnalyzeParameter(SyntaxNodeAnalysisContext syntaxNodeAnalysisContext) | ||
{ | ||
var fieldDeclarationSyntax = (FieldDeclarationSyntax)syntaxNodeAnalysisContext.Node; | ||
|
||
var semanticModel = syntaxNodeAnalysisContext.SemanticModel; | ||
var variableDeclarationSyntax = fieldDeclarationSyntax.Declaration; | ||
var type = variableDeclarationSyntax.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())); | ||
} | ||
} | ||
} | ||
} |
80 changes: 80 additions & 0 deletions
80
...s.GripeWithRoslyn.Analyzer/Analyzers/Runtime/DoNotUseObjectAsLocalVariableTypeAnalyzer.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 variable declaration. | ||
/// </summary> | ||
[DiagnosticAnalyzer(LanguageNames.CSharp)] | ||
public sealed class DoNotUseObjectAsLocalVariableTypeAnalyzer : DiagnosticAnalyzer | ||
{ | ||
internal const string Title = "Do not use Object in a variable 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="DoNotUseObjectAsLocalVariableTypeAnalyzer"/> class. | ||
/// </summary> | ||
public DoNotUseObjectAsLocalVariableTypeAnalyzer() | ||
{ | ||
_rule = new DiagnosticDescriptor( | ||
DiagnosticIdsHelper.DoNotUseObjectAsLocalVariableType, | ||
Title, | ||
MessageFormat, | ||
Category, | ||
DiagnosticSeverity.Warning, | ||
isEnabledByDefault: true, | ||
description: DiagnosticResultDescriptionFactory.DoNotUseObjectAsLocalVariableType()); | ||
} | ||
|
||
/// <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.VariableDeclaration); | ||
} | ||
|
||
private void AnalyzeParameter(SyntaxNodeAnalysisContext syntaxNodeAnalysisContext) | ||
{ | ||
var parameterSyntax = (VariableDeclarationSyntax)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())); | ||
} | ||
} | ||
} | ||
} |
80 changes: 80 additions & 0 deletions
80
src/Dhgms.GripeWithRoslyn.Analyzer/Analyzers/Runtime/DoNotUseObjectAsPropertyTypeAnalyzer.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 DoNotUseObjectAsPropertyTypeAnalyzer : DiagnosticAnalyzer | ||
{ | ||
internal const string Title = "Do not use Object in a property 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="DoNotUseObjectAsPropertyTypeAnalyzer"/> class. | ||
/// </summary> | ||
public DoNotUseObjectAsPropertyTypeAnalyzer() | ||
{ | ||
_rule = new DiagnosticDescriptor( | ||
DiagnosticIdsHelper.DoNotUseObjectAsPropertyType, | ||
Title, | ||
MessageFormat, | ||
Category, | ||
DiagnosticSeverity.Warning, | ||
isEnabledByDefault: true, | ||
description: DiagnosticResultDescriptionFactory.DoNotUseObjectAsPropertyType()); | ||
} | ||
|
||
/// <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.PropertyDeclaration); | ||
} | ||
|
||
private void AnalyzeParameter(SyntaxNodeAnalysisContext syntaxNodeAnalysisContext) | ||
{ | ||
var parameterSyntax = (PropertyDeclarationSyntax)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())); | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.