Skip to content

Commit

Permalink
REFL043 First argument must match type. Fix DotNetAnalyzers#180.
Browse files Browse the repository at this point in the history
Forgot to commit REFL043 separately hence mixed, don't feel like spending time untangling it.
  • Loading branch information
JohanLarsson committed Nov 3, 2018
1 parent 421e404 commit 155efec
Show file tree
Hide file tree
Showing 20 changed files with 654 additions and 76 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -172,5 +172,9 @@ Analyzers checking System.Reflection
<td><a href="https://github.com/DotNetAnalyzers/ReflectionAnalyzers/tree/master/documentation/REFL042.md">REFL042</a></td>
<td>First argument must be reference type.</td>
</tr>
<tr>
<td><a href="https://github.com/DotNetAnalyzers/ReflectionAnalyzers/tree/master/documentation/REFL043.md">REFL043</a></td>
<td>First argument must match type.</td>
</tr>
<table>
<!-- end generated table -->
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace ReflectionAnalyzers.Tests.REFL010PreferGenericGetCustomAttributeTests
internal class CodeFix
{
private static readonly DiagnosticAnalyzer Analyzer = new GetCustomAttributeAnalyzer();
private static readonly CodeFixProvider Fix = new GetCustomAttributeFix();
private static readonly CodeFixProvider Fix = new UseGenericGetCustomAttributeFix();
private static readonly ExpectedDiagnostic ExpectedDiagnostic = ExpectedDiagnostic.Create("REFL010");

[Test]
Expand Down
79 changes: 78 additions & 1 deletion ReflectionAnalyzers.Tests/REFL012PreferIsDefinedTests/CodeFix.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace ReflectionAnalyzers.Tests.REFL012PreferIsDefinedTests
internal class CodeFix
{
private static readonly DiagnosticAnalyzer Analyzer = new GetCustomAttributeAnalyzer();
private static readonly CodeFixProvider Fix = new GetCustomAttributeFix();
private static readonly CodeFixProvider Fix = new UseIsDefinedFix();
private static readonly ExpectedDiagnostic ExpectedDiagnostic = ExpectedDiagnostic.Create(REFL012PreferIsDefined.DiagnosticId);

[Test]
Expand Down Expand Up @@ -92,5 +92,82 @@ class C
var message = "Prefer Attribute.IsDefined().";
AnalyzerAssert.CodeFix(Analyzer, Fix, ExpectedDiagnostic.WithMessage(message), code, fixedCode);
}

[TestCase(" == null")]
[TestCase(" is null")]
public void IfGetCustomAttributeIsNull(string isNull)
{
var code = @"
namespace RoslynSandbox
{
using System;
using System.Reflection;
class C
{
public C()
{
if (typeof(C).GetCustomAttribute(typeof(ObsoleteAttribute)) == null)
{
}
}
}
}".AssertReplace(" == null", isNull);
var fixedCode = @"
namespace RoslynSandbox
{
using System;
using System.Reflection;
class C
{
public C()
{
if (!typeof(C).IsDefined(typeof(ObsoleteAttribute)))
{
}
}
}
}";
AnalyzerAssert.CodeFix(Analyzer, Fix, ExpectedDiagnostic, code, fixedCode);
}

[Test]
public void IfGetCustomAttributeNotNull()
{
var code = @"
namespace RoslynSandbox
{
using System;
using System.Reflection;
class C
{
public C()
{
if (typeof(C).GetCustomAttribute(typeof(ObsoleteAttribute)) != null)
{
}
}
}
}";
var fixedCode = @"
namespace RoslynSandbox
{
using System;
using System.Reflection;
class C
{
public C()
{
if (typeof(C).IsDefined(typeof(ObsoleteAttribute)))
{
}
}
}
}";
AnalyzerAssert.CodeFix(Analyzer, Fix, ExpectedDiagnostic, code, fixedCode);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace ReflectionAnalyzers.Tests.REFL042FirstArgumentMustBeReferenceTypeTests
public class Diagnostics
{
private static readonly DiagnosticAnalyzer Analyzer = new CreateDelegateAnalyzer();
private static readonly ExpectedDiagnostic ExpectedDiagnostic = ExpectedDiagnostic.Create(REFL042FirstArgumentMustBeReferenceType.Descriptor);
private static readonly ExpectedDiagnostic ExpectedDiagnostic = ExpectedDiagnostic.Create(REFL042FirstArgumentIsReferenceType.Descriptor);

[Test]
public void StaticStringVoidFirstArg()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace ReflectionAnalyzers.Tests.REFL042FirstArgumentMustBeReferenceTypeTests
public class ValidCode
{
private static readonly DiagnosticAnalyzer Analyzer = new CreateDelegateAnalyzer();
private static readonly DiagnosticDescriptor Descriptor = REFL042FirstArgumentMustBeReferenceType.Descriptor;
private static readonly DiagnosticDescriptor Descriptor = REFL042FirstArgumentIsReferenceType.Descriptor;

[Test]
public void StaticStringIntWithFirstArg()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
namespace ReflectionAnalyzers.Tests.REFL043FirstArgumentTypeTests
{
using Gu.Roslyn.Asserts;
using Microsoft.CodeAnalysis.Diagnostics;
using NUnit.Framework;

public class Diagnostics
{
private static readonly DiagnosticAnalyzer Analyzer = new CreateDelegateAnalyzer();
private static readonly ExpectedDiagnostic ExpectedDiagnostic = ExpectedDiagnostic.Create(REFL043FirstArgumentType.Descriptor);

[Test]
public void StaticStringVoidFirstArg()
{
var code = @"
namespace RoslynSandbox
{
using System;
using System.Reflection;
class C
{
public static void M(string arg) { }
public static object Get => Delegate.CreateDelegate(
typeof(Action),
new C(),
typeof(C).GetMethod(nameof(M)));
}
}";

AnalyzerAssert.Diagnostics(Analyzer, ExpectedDiagnostic, code);
}
}
}
Loading

0 comments on commit 155efec

Please sign in to comment.