提交 798e642d 编写于 作者: D Dustin Campbell

Merge pull request #8195 from steveniles/master

Bug fix for issue #8111 - Don't mark cast as redundant if removing it will change the shape of a new anonymous object
......@@ -3790,5 +3790,50 @@ private static void RequiresCondition<TException>(bool condition, string message
}
}");
}
[WorkItem(8111, "https://github.com/dotnet/roslyn/issues/8111")]
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
public async Task DontRemoveCastThatChangesShapeOfAnonymousTypeObject()
{
await TestMissingAsync(
@"
class Program
{
static void Main(string[] args)
{
object thing = new { shouldBeAnInt = [|(int)Directions.South|] };
}
public enum Directions { North, East, South, West }
}
");
}
[WorkItem(8111, "https://github.com/dotnet/roslyn/issues/8111")]
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsRemoveUnnecessaryCast)]
public async Task RemoveCastThatDoesntChangeShapeOfAnonymousTypeObject()
{
await TestAsync(
@"
class Program
{
static void Main(string[] args)
{
object thing = new { shouldBeAnInt = [|(Directions)Directions.South|] };
}
public enum Directions { North, East, South, West }
}
",
@"
class Program
{
static void Main(string[] args)
{
object thing = new { shouldBeAnInt = Directions.South };
}
public enum Directions { North, East, South, West }
}
");
}
}
}
......@@ -316,6 +316,34 @@ protected IRogueAction(string name)
} ", "Name", semanticChanges: false, isBrokenCode: true);
}
[Fact, WorkItem(8111, "https://github.com/dotnet/roslyn/issues/8111")]
public void SpeculationAnalyzerAnonymousObjectMemberDeclaredWithNeededCast()
{
Test(@"
class Program
{
static void Main(string[] args)
{
object thing = new { shouldBeAnInt = [|(int)Directions.South|] };
}
public enum Directions { North, East, South, West }
} ", "Directions.South", semanticChanges: true);
}
[Fact, WorkItem(8111, "https://github.com/dotnet/roslyn/issues/8111")]
public void SpeculationAnalyzerAnonymousObjectMemberDeclaredWithUnneededCast()
{
Test(@"
class Program
{
static void Main(string[] args)
{
object thing = new { shouldBeAnInt = [|(Directions)Directions.South|] };
}
public enum Directions { North, East, South, West }
} ", "Directions.South", semanticChanges: false);
}
protected override SyntaxTree Parse(string text)
{
return SyntaxFactory.ParseSyntaxTree(text);
......
......@@ -431,10 +431,23 @@ protected override bool ReplacementChangesSemanticsForNodeLanguageSpecific(Synta
{
return !TypesAreCompatible((ImplicitArrayCreationExpressionSyntax)currentOriginalNode, (ImplicitArrayCreationExpressionSyntax)currentReplacedNode);
}
else if (currentOriginalNode is AnonymousObjectMemberDeclaratorSyntax)
{
var originalAnonymousObjectMemberDeclarator = (AnonymousObjectMemberDeclaratorSyntax)currentOriginalNode;
var replacedAnonymousObjectMemberDeclarator = (AnonymousObjectMemberDeclaratorSyntax)currentReplacedNode;
return ReplacementBreaksAnonymousObjectMemberDeclarator(originalAnonymousObjectMemberDeclarator, replacedAnonymousObjectMemberDeclarator);
}
return false;
}
private bool ReplacementBreaksAnonymousObjectMemberDeclarator(AnonymousObjectMemberDeclaratorSyntax originalAnonymousObjectMemberDeclarator, AnonymousObjectMemberDeclaratorSyntax replacedAnonymousObjectMemberDeclarator)
{
var originalExpressionType = this.OriginalSemanticModel.GetTypeInfo(originalAnonymousObjectMemberDeclarator.Expression, this.CancellationToken).Type;
var newExpressionType = this.SpeculativeSemanticModel.GetTypeInfo(replacedAnonymousObjectMemberDeclarator.Expression, this.CancellationToken).Type;
return originalExpressionType != newExpressionType;
}
private bool ReplacementBreaksConstructorInitializer(ConstructorInitializerSyntax ctorInitializer, ConstructorInitializerSyntax newCtorInitializer)
{
var originalSymbol = this.OriginalSemanticModel.GetSymbolInfo(ctorInitializer, CancellationToken).Symbol;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册