Skip to content

Commit

Permalink
Merge pull request #407 from Hosch250/UseAliasesBugs
Browse files Browse the repository at this point in the history
Close #400 and #406
  • Loading branch information
Vannevelj committed Feb 22, 2016
2 parents a70dbdb + f34492c commit 21686d0
Show file tree
Hide file tree
Showing 3 changed files with 170 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1075,7 +1075,6 @@ public class Program
VerifyFix(original, result, allowNewCompilerDiagnostics: true);
}


[TestMethod]
public void UseAliasesInsteadOfConcreteType_Int32BecomesInt_Int32DotMaxValue()
{
Expand Down Expand Up @@ -1453,5 +1452,157 @@ void Method()
VerifyDiagnostic(original, string.Format(UseAliasesInsteadOfConcreteTypeAnalyzer.Rule.MessageFormat.ToString(), "string", "String"));
VerifyFix(original, result, allowNewCompilerDiagnostics: true);
}

[TestMethod]
public void UseAliasesInsteadOfConcreteType_DoesNotCatchLinqSingle()
{
var original = @"
using System;
using System.Collections.Generic;
using System.Linq;
namespace ConsoleApplication1
{
class MyClass
{
void Method()
{
List<string> list = new List<string> { ""test"", ""test1"" };
string str = list.Single(s => s == ""test"");
}
}
}";

VerifyDiagnostic(original);
}

[TestMethod]
public void UseAliasesInsteadOfConcreteType_DoesNotCatchEnumValuesWithSameNameAsAliasableType()
{
var original = @"
using System;
using System.Linq;
namespace ConsoleApplication1
{
public enum TypeCode
{
Object = 1,
DBNull = 2,
Boolean = 3,
Char = 4,
SByte = 5,
Byte = 6,
}
class MyClass
{
void Method()
{
var type = TypeCode.Boolean;
if (type == TypeCode.Boolean) {}
}
}
}";

VerifyDiagnostic(original);
}

[TestMethod]
public void UseAliasesInsteadOfConcreteType_DoesNotCatchVariablesWithSameNameAsAliasableType()
{
var original = @"
using System;
using System.Linq;
namespace ConsoleApplication1
{
class MyClass
{
void Method()
{
var Boolean = ""Boolean"";
}
}
}";

VerifyDiagnostic(original);
}

[TestMethod]
public void UseAliasesInsteadOfConcreteType_DoesNotCatchClassWithSameNameAsAliasableType()
{
var original = @"
using System;
namespace ConsoleApplication1
{
class Single
{
}
class Foo
{
void Method()
{
Single bar = new Single();
}
}
}";

VerifyDiagnostic(original);
}

[TestMethod]
public void UseAliasesInsteadOfConcreteType_DoesNotCatchUsingDirective()
{
var original = @"
using Single = System.Single;
namespace ConsoleApplication1
{
class Foo
{
static void Main() { }
}
}";

VerifyDiagnostic(original);
}

[TestMethod]
public void UseAliasesInsteadOfConcreteType_DisplaysCorrectWarning()
{
var original = @"
using Single = System.String;
namespace ConsoleApplication1
{
class Foo
{
static void Main()
{
Single bar = ""test"";
}
}
}";

var result = @"
using Single = System.String;
namespace ConsoleApplication1
{
class Foo
{
static void Main()
{
string bar = ""test"";
}
}
}";

VerifyDiagnostic(original, string.Format(UseAliasesInsteadOfConcreteTypeAnalyzer.Rule.MessageFormat.ToString(), "string", "String"));
VerifyFix(original, result, allowNewCompilerDiagnostics: true);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ private void AnalyzeSymbol(SyntaxNodeAnalysisContext context)
}

var typeSymbol = identifierSymbol.Symbol as INamedTypeSymbol;
if (typeSymbol != null && typeSymbol.SpecialType == SpecialType.None)
if (typeSymbol == null || typeSymbol.SpecialType == SpecialType.None)
{
return;
}
Expand All @@ -60,15 +60,20 @@ private void AnalyzeSymbol(SyntaxNodeAnalysisContext context)
// This will make sure that we accept the entire qualified name in the code fix
var location = identifier.GetLocation();
var qualifiedName = identifier.AncestorsAndSelf().OfType<QualifiedNameSyntax>().FirstOrDefault();
if (qualifiedName?.Parent is UsingDirectiveSyntax)
{
return;
}

if (qualifiedName != null)
{
location = qualifiedName.GetLocation();
}

string alias;
if (identifier.Identifier.Text.HasAlias(out alias))
if (identifier.Identifier.Text.HasAlias() && typeSymbol.MetadataName.HasAlias(out alias))
{
context.ReportDiagnostic(Diagnostic.Create(Rule, location, alias, identifier.Identifier.ValueText));
context.ReportDiagnostic(Diagnostic.Create(Rule, location, alias, typeSymbol.MetadataName));
}
}
}
Expand Down
10 changes: 10 additions & 0 deletions VSDiagnostics/VSDiagnostics/VSDiagnostics/Utilities/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,16 @@ public static bool HasAlias(this string type, out string alias)
return AliasMapping.TryGetValue(type, out alias);
}

public static bool HasAlias(this string type)
{
if (type == null)
{
throw new ArgumentNullException(nameof(type));
}

return AliasMapping.Keys.Contains(type);
}

/// <summary>
/// Determines whether or not the specified <see cref="IMethodSymbol" /> is the symbol of an asynchronous method. This
/// can be a method declared as async (e.g. returning <see cref="Task" /> or <see cref="Task{TResult}" />), or a method
Expand Down

0 comments on commit 21686d0

Please sign in to comment.