-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
REFL024 Prefer null over empty array. Fix #18.
- Loading branch information
1 parent
b4f9499
commit 8945c88
Showing
9 changed files
with
227 additions
and
3 deletions.
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
File renamed without changes.
54 changes: 54 additions & 0 deletions
54
ReflectionAnalyzers.Tests/REFL024PreferNullOverEmptyArrayTests/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,54 @@ | ||
namespace ReflectionAnalyzers.Tests.REFL024PreferNullOverEmptyArrayTests | ||
{ | ||
using Gu.Roslyn.Asserts; | ||
using Microsoft.CodeAnalysis.CodeFixes; | ||
using Microsoft.CodeAnalysis.Diagnostics; | ||
using NUnit.Framework; | ||
using ReflectionAnalyzers.Codefixes; | ||
|
||
public class CodeFix | ||
{ | ||
private static readonly DiagnosticAnalyzer Analyzer = new InvokeAnalyzer(); | ||
private static readonly CodeFixProvider Fix = new PreferNullFix(); | ||
private static readonly ExpectedDiagnostic ExpectedDiagnostic = ExpectedDiagnostic.Create(REFL024PreferNullOverEmptyArray.Descriptor); | ||
|
||
[TestCase("Array.Empty<object>()")] | ||
[TestCase("new object[0]")] | ||
[TestCase("new object[0] { }")] | ||
[TestCase("new object[] { }")] | ||
public void MemberInfoInvoke(string emptyArray) | ||
{ | ||
var code = @" | ||
namespace RoslynSandbox | ||
{ | ||
using System; | ||
using System.Reflection; | ||
public class Foo | ||
{ | ||
public Foo(MethodInfo member) | ||
{ | ||
_ = member.Invoke(null, Array.Empty<object>()); | ||
} | ||
} | ||
}".AssertReplace("Array.Empty<object>()", emptyArray); | ||
|
||
var fixedCode = @" | ||
namespace RoslynSandbox | ||
{ | ||
using System; | ||
using System.Reflection; | ||
public class Foo | ||
{ | ||
public Foo(MethodInfo member) | ||
{ | ||
_ = member.Invoke(null, null); | ||
} | ||
} | ||
}"; | ||
|
||
AnalyzerAssert.CodeFix(Analyzer, Fix, ExpectedDiagnostic, code, fixedCode); | ||
} | ||
} | ||
} |
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,38 @@ | ||
namespace ReflectionAnalyzers.Codefixes | ||
{ | ||
using System.Collections.Immutable; | ||
using System.Composition; | ||
using System.Threading.Tasks; | ||
using Gu.Roslyn.CodeFixExtensions; | ||
using Microsoft.CodeAnalysis; | ||
using Microsoft.CodeAnalysis.CodeFixes; | ||
using Microsoft.CodeAnalysis.CSharp; | ||
using Microsoft.CodeAnalysis.CSharp.Syntax; | ||
|
||
[ExportCodeFixProvider(LanguageNames.CSharp, Name = nameof(PreferNullFix))] | ||
[Shared] | ||
internal class PreferNullFix : DocumentEditorCodeFixProvider | ||
{ | ||
public override ImmutableArray<string> FixableDiagnosticIds { get; } = ImmutableArray.Create( | ||
REFL024PreferNullOverEmptyArray.DiagnosticId); | ||
|
||
protected override async Task RegisterCodeFixesAsync(DocumentEditorCodeFixContext context) | ||
{ | ||
var syntaxRoot = await context.Document.GetSyntaxRootAsync(context.CancellationToken) | ||
.ConfigureAwait(false); | ||
foreach (var diagnostic in context.Diagnostics) | ||
{ | ||
if (syntaxRoot.TryFindNode(diagnostic, out ArgumentSyntax argument)) | ||
{ | ||
context.RegisterCodeFix( | ||
"Prefer null.", | ||
(editor, _) => editor.ReplaceNode( | ||
argument.Expression, | ||
x => SyntaxFactory.LiteralExpression(SyntaxKind.NullLiteralExpression)), | ||
nameof(PreferNullFix), | ||
diagnostic); | ||
} | ||
} | ||
} | ||
} | ||
} |
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 REFL024PreferNullOverEmptyArray | ||
{ | ||
public const string DiagnosticId = "REFL024"; | ||
|
||
internal static readonly DiagnosticDescriptor Descriptor = new DiagnosticDescriptor( | ||
id: DiagnosticId, | ||
title: "Prefer null over empty array.", | ||
messageFormat: "Prefer null over empty array.", | ||
category: AnalyzerCategory.SystemReflection, | ||
defaultSeverity: DiagnosticSeverity.Warning, | ||
isEnabledByDefault: true, | ||
description: "Prefer null over empty array.", | ||
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 @@ | ||
# REFL024 | ||
## Prefer null over empty array. | ||
|
||
<!-- start generated table --> | ||
<table> | ||
<tr> | ||
<td>CheckId</td> | ||
<td>REFL024</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/InvokeAnalyzer.cs">InvokeAnalyzer</a></td> | ||
</tr> | ||
</table> | ||
<!-- end generated table --> | ||
|
||
## Description | ||
|
||
Prefer null over empty array. | ||
|
||
## 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 REFL024 // Prefer null over empty array. | ||
Code violating the rule here | ||
#pragma warning restore REFL024 // Prefer null over empty array. | ||
``` | ||
|
||
Or put this at the top of the file to disable all instances. | ||
```C# | ||
#pragma warning disable REFL024 // Prefer null over empty array. | ||
``` | ||
|
||
### Via attribute `[SuppressMessage]`. | ||
|
||
```C# | ||
[System.Diagnostics.CodeAnalysis.SuppressMessage("ReflectionAnalyzers.SystemReflection", | ||
"REFL024:Prefer null over empty array.", | ||
Justification = "Reason...")] | ||
``` | ||
<!-- end generated config severity --> |