Skip to content
This repository has been archived by the owner. It is now read-only.

Commit

Permalink
Browse files Browse the repository at this point in the history
Improved RedundantExplicitNullableCreationAnalyzer: Now not graying o…
…ut the expression left after applying fix, fixed wrong formatting produced by fix, removed old comments.
  • Loading branch information
Rpinski committed Sep 29, 2015
1 parent 644b79c commit bc44246
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 40 deletions.
Expand Up @@ -2,6 +2,7 @@
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Diagnostics;
using System.Collections.Generic;
using System.Collections.Immutable;

namespace RefactoringEssentials.CSharp.Diagnostics
Expand Down Expand Up @@ -47,52 +48,20 @@ private static bool TryGetRedundantNullableDiagnostic(SyntaxNodeAnalysisContext
if (objectCreation == null)
return false;

//Not so sure about this check but was there before
// No Nullable, but "var"
var parentVarDeclaration = objectCreation?.Parent?.Parent?.Parent as VariableDeclarationSyntax;
if (parentVarDeclaration != null && parentVarDeclaration.Type.IsVar)
return false;

var objectCreationSymbol = nodeContext.SemanticModel.GetTypeInfo(objectCreation);
//IsNullable method fails the analysis
//Used string comparison for testing even though it's bad on performance.
if (objectCreationSymbol.Type != null && objectCreationSymbol.Type.OriginalDefinition.ToString().Equals("System.Nullable<T>"))
if (objectCreationSymbol.Type != null && objectCreationSymbol.Type.IsNullableType())
{
diagnostic = Diagnostic.Create(descriptor, objectCreation.GetLocation());
var creationTypeLocation = objectCreation.Type.GetLocation();
var newKeywordLocation = objectCreation.NewKeyword.GetLocation();
diagnostic = Diagnostic.Create(descriptor, newKeywordLocation, (IEnumerable<Location>) (new[] { creationTypeLocation }));
return true;
}
return false;
}

// class GatherVisitor : GatherVisitorBase<RedundantExplicitNullableCreationAnalyzer>
// {
// public GatherVisitor(SemanticModel semanticModel, Action<Diagnostic> addDiagnostic, CancellationToken cancellationToken)
// : base (semanticModel, addDiagnostic, cancellationToken)
// {
// }

//// public override void VisitObjectCreateExpression(ObjectCreateExpression objectCreateExpression)
//// {
//// base.VisitObjectCreateExpression(objectCreateExpression);
////
//// // test for var foo = new ...
//// var parentVarDecl = objectCreateExpression.Parent.Parent as VariableDeclarationStatement;
//// if (parentVarDecl != null && parentVarDecl.Type.IsVar())
//// return;
////
//// var rr = ctx.Resolve(objectCreateExpression);
//// if (!NullableType.IsNullable(rr.Type))
//// return;
//// var arg = objectCreateExpression.Arguments.FirstOrDefault();
//// if (arg == null)
//// return;
//// AddDiagnosticAnalyzer(new CodeIssue(
//// objectCreateExpression.StartLocation,
//// objectCreateExpression.Type.EndLocation,
//// ctx.TranslateString(""),
//// ctx.TranslateString(""),
//// s => s.Replace(objectCreateExpression, arg.Clone())
//// ) { IssueMarker = IssueMarker.GrayOut });
//// }
// }
}
}
Expand Up @@ -40,7 +40,8 @@ public async override Task RegisterCodeFixesAsync(CodeFixContext context)

context.RegisterCodeFix(CodeActionFactory.Create(objectCreation.Span, diagnostic.Severity, "Redundant explicit nullable type creation", token =>
{
var newRoot = root.ReplaceNode(objectCreation, argumentListArgument.Expression.WithAdditionalAnnotations(Formatter.Annotation));
var newRoot = root.ReplaceNode(objectCreation,
argumentListArgument.Expression.WithLeadingTrivia(objectCreation.GetLeadingTrivia()).WithAdditionalAnnotations(Formatter.Annotation));
return Task.FromResult(document.WithSyntaxRoot(newRoot));
}), diagnostic);
Expand Down
Expand Up @@ -14,7 +14,7 @@ class FooBar
{
void Test()
{
int? i = $new int?(5)$;
int? i = $new int?$(5);
}
}
", @"
Expand All @@ -36,7 +36,7 @@ class FooBar
{
void Test()
{
int? i = $new System.Nullable<int>(5)$;
int? i = $new System.Nullable<int>$(5);
}
}
", @"
Expand Down

0 comments on commit bc44246

Please sign in to comment.