forked from DotNetAnalyzers/ReflectionAnalyzers
-
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.
REFL044 Expected attribute type. DotNetAnalyzers#182.
- Loading branch information
1 parent
155efec
commit be33eb7
Showing
7 changed files
with
203 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
30 changes: 30 additions & 0 deletions
30
ReflectionAnalyzers.Tests/REFL044ExpectedAttributeTypeTests/CodeFix.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,30 @@ | ||
namespace ReflectionAnalyzers.Tests.REFL044ExpectedAttributeTypeTests | ||
{ | ||
using Gu.Roslyn.Asserts; | ||
using Microsoft.CodeAnalysis.Diagnostics; | ||
using NUnit.Framework; | ||
|
||
internal class CodeFix | ||
{ | ||
private static readonly DiagnosticAnalyzer Analyzer = new GetCustomAttributeAnalyzer(); | ||
private static readonly ExpectedDiagnostic ExpectedDiagnostic = ExpectedDiagnostic.Create(REFL044ExpectedAttributeType.DiagnosticId); | ||
|
||
[Test] | ||
public void AttributeGetCustomAttribute() | ||
{ | ||
var code = @" | ||
namespace RoslynSandbox | ||
{ | ||
using System; | ||
class C | ||
{ | ||
public static bool Bar() => Attribute.GetCustomAttribute(typeof(C), ↓typeof(string)) == null; | ||
} | ||
}"; | ||
|
||
var message = "Expected attribute type."; | ||
AnalyzerAssert.Diagnostics(Analyzer, ExpectedDiagnostic.WithMessage(message), code); | ||
} | ||
} | ||
} |
72 changes: 72 additions & 0 deletions
72
ReflectionAnalyzers.Tests/REFL044ExpectedAttributeTypeTests/ValidCode.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,72 @@ | ||
namespace ReflectionAnalyzers.Tests.REFL044ExpectedAttributeTypeTests | ||
{ | ||
using Gu.Roslyn.Asserts; | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.Diagnostics; | ||
using NUnit.Framework; | ||
|
||
internal class ValidCode | ||
{ | ||
private static readonly DiagnosticAnalyzer Analyzer = new GetCustomAttributeAnalyzer(); | ||
private static readonly DiagnosticDescriptor Descriptor = REFL044ExpectedAttributeType.Descriptor; | ||
|
||
[Test] | ||
public void WhenUsingGeneric() | ||
{ | ||
var code = @" | ||
namespace RoslynSandbox | ||
{ | ||
using System; | ||
using System.Reflection; | ||
class C | ||
{ | ||
public C() | ||
{ | ||
var attribute = typeof(C).GetCustomAttribute<ObsoleteAttribute>(); | ||
} | ||
} | ||
}"; | ||
AnalyzerAssert.Valid(Analyzer, Descriptor, code); | ||
} | ||
|
||
[Test] | ||
public void WhenGetCustomAttributeCast() | ||
{ | ||
var code = @" | ||
namespace RoslynSandbox | ||
{ | ||
using System; | ||
class C | ||
{ | ||
public C() | ||
{ | ||
var attribute = (ObsoleteAttribute)Attribute.GetCustomAttribute(typeof(C), typeof(ObsoleteAttribute)); | ||
} | ||
} | ||
}"; | ||
AnalyzerAssert.Valid(Analyzer, Descriptor, code); | ||
} | ||
|
||
[Test] | ||
public void WhenGetCustomAttributeAs() | ||
{ | ||
var code = @" | ||
namespace RoslynSandbox | ||
{ | ||
using System; | ||
class C | ||
{ | ||
public C() | ||
{ | ||
var attribute = Attribute.GetCustomAttribute(typeof(C), typeof(ObsoleteAttribute)) as ObsoleteAttribute; | ||
} | ||
} | ||
}"; | ||
|
||
AnalyzerAssert.Valid(Analyzer, Descriptor, code); | ||
} | ||
} | ||
} |
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
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,19 @@ | ||
namespace ReflectionAnalyzers | ||
{ | ||
using Microsoft.CodeAnalysis; | ||
|
||
internal static class REFL044ExpectedAttributeType | ||
{ | ||
internal const string DiagnosticId = "REFL044"; | ||
|
||
internal static readonly DiagnosticDescriptor Descriptor = new DiagnosticDescriptor( | ||
id: DiagnosticId, | ||
title: "Expected attribute type.", | ||
messageFormat: "Expected attribute type.", | ||
category: AnalyzerCategory.SystemReflection, | ||
defaultSeverity: DiagnosticSeverity.Warning, | ||
isEnabledByDefault: true, | ||
description: "Expected attribute type.", | ||
helpLinkUri: HelpLink.ForId(DiagnosticId)); | ||
} | ||
} |
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,67 @@ | ||
# REFL044 | ||
## Expected attribute type. | ||
|
||
<!-- start generated table --> | ||
<table> | ||
<tr> | ||
<td>CheckId</td> | ||
<td>REFL044</td> | ||
</tr> | ||
<tr> | ||
<td>Severity</td> | ||
<td>Warning</td> | ||
</tr> | ||
<tr> | ||
<td>Enabled</td> | ||
<td>true</td> | ||
</tr> | ||
<tr> | ||
<td>Category</td> | ||
<td>ReflectionAnalyzers.SystemReflection</td> | ||
</tr> | ||
<tr> | ||
<td>Code</td> | ||
<td><a href="https://github.com/DotNetAnalyzers/ReflectionAnalyzers/blob/master/ReflectionAnalyzers/NodeAnalzers/GetCustomAttributeAnalyzer.cs">GetCustomAttributeAnalyzer</a></td> | ||
</tr> | ||
</table> | ||
<!-- end generated table --> | ||
|
||
## Description | ||
|
||
Expected attribute type. | ||
|
||
## Motivation | ||
|
||
ADD MOTIVATION HERE | ||
|
||
## How to fix violations | ||
|
||
ADD HOW TO FIX VIOLATIONS HERE | ||
|
||
<!-- start generated config severity --> | ||
## Configure severity | ||
|
||
### Via ruleset file. | ||
|
||
Configure the severity per project, for more info see [MSDN](https://msdn.microsoft.com/en-us/library/dd264949.aspx). | ||
|
||
### Via #pragma directive. | ||
```C# | ||
#pragma warning disable REFL044 // Expected attribute type. | ||
Code violating the rule here | ||
#pragma warning restore REFL044 // Expected attribute type. | ||
``` | ||
|
||
Or put this at the top of the file to disable all instances. | ||
```C# | ||
#pragma warning disable REFL044 // Expected attribute type. | ||
``` | ||
|
||
### Via attribute `[SuppressMessage]`. | ||
|
||
```C# | ||
[System.Diagnostics.CodeAnalysis.SuppressMessage("ReflectionAnalyzers.SystemReflection", | ||
"REFL044:Expected attribute type.", | ||
Justification = "Reason...")] | ||
``` | ||
<!-- end generated config severity --> |