Fix issues with 'Remove unnecessary cast'#42883
Conversation
| // The .NET Foundation licenses this file to you under the MIT license. | ||
| // See the LICENSE file in the project root for more information. | ||
|
|
||
| using System.Linq; |
There was a problem hiding this comment.
stale file. likely a merge conflict when we moved things to the shared layer.
...es/SharedUtilitiesAndExtensions/Compiler/CSharp/Simplification/Simplifiers/CastSimplifier.cs
Show resolved
Hide resolved
src/Analyzers/CSharp/Tests/RemoveUnnecessaryCast/RemoveUnnecessaryCastTests.cs
Show resolved
Hide resolved
src/Analyzers/CSharp/Tests/RemoveUnnecessaryCast/RemoveUnnecessaryCastTests.cs
Outdated
Show resolved
Hide resolved
src/Analyzers/CSharp/Tests/RemoveUnnecessaryCast/RemoveUnnecessaryCastTests.cs
Outdated
Show resolved
Hide resolved
| return false; | ||
|
|
||
| // Don't remove any explicit numeric casts. | ||
| // https://github.com/dotnet/roslyn/issues/2987 tracks improving on this conservative approach. |
There was a problem hiding this comment.
❔ Comment still necessary?
...es/SharedUtilitiesAndExtensions/Compiler/CSharp/Simplification/Simplifiers/CastSimplifier.cs
Show resolved
Hide resolved
| if (castedExpressionNode.WalkDownParentheses().IsKind(SyntaxKind.DefaultLiteralExpression) && | ||
| !castType.Equals(outerType) && | ||
| outerType.IsNullable()) |
There was a problem hiding this comment.
😕 It's hard to relate this condition to the one it tests for. I'll need to come back to it.
src/Workspaces/SharedUtilitiesAndExtensions/Compiler/Core/Extensions/ISymbolExtensions.cs
Show resolved
Hide resolved
Please update that issue to explain how this improvement was approached. |
...es/SharedUtilitiesAndExtensions/Compiler/CSharp/Simplification/Simplifiers/CastSimplifier.cs
Outdated
Show resolved
Hide resolved
...es/SharedUtilitiesAndExtensions/Compiler/CSharp/Simplification/Simplifiers/CastSimplifier.cs
Outdated
Show resolved
Hide resolved
...es/SharedUtilitiesAndExtensions/Compiler/CSharp/Simplification/Simplifiers/CastSimplifier.cs
Outdated
Show resolved
Hide resolved
| // Switch expression's expression changed. Ensure it's the same type as before. If not, inference of | ||
| // the meaning of the patterns within can change. |
There was a problem hiding this comment.
❔ Do we not need to check the patterns, like we do for switch statements?
There was a problem hiding this comment.
it's quite possble we need to for total correctness. i'm not certain at this point. going to leave it alone for now :)
...rkspaces/SharedUtilitiesAndExtensions/Compiler/Core/Utilities/AbstractSpeculationAnalyzer.cs
Show resolved
Hide resolved
...rkspaces/SharedUtilitiesAndExtensions/Compiler/Core/Utilities/AbstractSpeculationAnalyzer.cs
Show resolved
Hide resolved
...rkspaces/SharedUtilitiesAndExtensions/Compiler/Core/Utilities/AbstractSpeculationAnalyzer.cs
Show resolved
Hide resolved
...rkspaces/SharedUtilitiesAndExtensions/Compiler/Core/Utilities/AbstractSpeculationAnalyzer.cs
Show resolved
Hide resolved
…mplification/Simplifiers/CastSimplifier.cs Co-Authored-By: Sam Harwell <sam@tunnelvisionlabs.com>
…mplification/Simplifiers/CastSimplifier.cs Co-Authored-By: Sam Harwell <sam@tunnelvisionlabs.com>
…mplification/Simplifiers/CastSimplifier.cs Co-Authored-By: Sam Harwell <sam@tunnelvisionlabs.com>
| public int Compare(object x, object y) | ||
| { | ||
| return this.x.Compare(x, y); | ||
| return ((IComparer)this.x).Compare(x, y); |
There was a problem hiding this comment.
📝 I'm OK with the approach taken in this pull request to put us back on a more conservative path, but we should file a follow-up issue to consider ways to improve this scenario in the future.
Fixes #41433
Fixes #38599
Fixes #32491
Fixes #25021
Fixes #2987
Fixes #38347
Fixes #36631
Fixes #34326
Fixes #29726
Fixes #6309
Fixes #34873
Fixes #37953
Fixes #42657
Fixes #42108
This PR should be reviewed one commit at a time.