-
-
Notifications
You must be signed in to change notification settings - Fork 290
Fix/RCS1031, RCS1208 and RCS1061 #1062
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
josefpihrt
merged 17 commits into
dotnet:main
from
jamesHargreaves12:fix/RCS1031-and-RCS1208
Apr 21, 2023
Merged
Changes from 11 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
60e0270
with type fixes; RCS1208 also check case pattern match vars
jamesHargreaves12 cde590b
self review changes
jamesHargreaves12 b98c4de
add loops and switch handling
jamesHargreaves12 849bb8f
update change log
jamesHargreaves12 30135c9
Merge branch 'main' into fix/RCS1031-and-RCS1208
jamesHargreaves12 f65b5b5
also fix RCS1061
jamesHargreaves12 8acacf9
update change log
jamesHargreaves12 ca03288
update change log
jamesHargreaves12 8f8eb6a
Merge branch 'main' into fix/RCS1031-and-RCS1208
jamesHargreaves12 d505987
Merge branch 'main' into fix/RCS1031-and-RCS1208
jamesHargreaves12 b7e2434
CR comments
jamesHargreaves12 d0ce250
more CR comments
jamesHargreaves12 999e277
Merge branch 'main' into fix/RCS1031-and-RCS1208
jamesHargreaves12 ce738a0
format
jamesHargreaves12 a0ca029
diagnostic fixes
jamesHargreaves12 74b1b88
CR comment
jamesHargreaves12 94323c7
Fix code style
josefpihrt File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
...alyzers/CSharp/Analysis/PattenMatchingAnalysis/PattenMatchingVariableDeclarationHelper.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| // Copyright (c) Josef Pihrt and Contributors. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
|
||
| using System.Collections.Immutable; | ||
| using Microsoft.CodeAnalysis.CSharp.Syntax; | ||
|
|
||
| namespace Roslynator.CSharp.Analysis; | ||
|
|
||
| public static class PattenMatchingVariableDeclarationHelper | ||
| { | ||
| public static bool AnyDeclaredVariablesMatch(PatternSyntax pattern, ImmutableHashSet<string> variableNames) | ||
| { | ||
| return pattern switch | ||
| { | ||
| RecursivePatternSyntax { PositionalPatternClause: var positionalPatternClause, PropertyPatternClause: var propertyPatternClause, Designation: var designation } => | ||
| (designation != null && AnyDeclaredVariablesMatch(designation, variableNames)) | ||
| || (propertyPatternClause != null && propertyPatternClause.Subpatterns.Any(p => AnyDeclaredVariablesMatch(p.Pattern, variableNames))) | ||
| || (positionalPatternClause != null && positionalPatternClause.Subpatterns.Any(p => AnyDeclaredVariablesMatch(p.Pattern, variableNames))), | ||
| BinaryPatternSyntax binaryPattern => | ||
| AnyDeclaredVariablesMatch(binaryPattern.Left, variableNames) | ||
| || AnyDeclaredVariablesMatch(binaryPattern.Right, variableNames), | ||
| ParenthesizedPatternSyntax parenthesizedPattern => AnyDeclaredVariablesMatch(parenthesizedPattern.Pattern, variableNames), | ||
| DeclarationPatternSyntax { Designation: var variableDesignation } => AnyDeclaredVariablesMatch(variableDesignation, variableNames), | ||
| VarPatternSyntax { Designation: var variableDesignation } => AnyDeclaredVariablesMatch(variableDesignation, variableNames), | ||
| _ => false | ||
| }; | ||
| } | ||
|
|
||
| internal static bool AnyDeclaredVariablesMatch(VariableDesignationSyntax designation, ImmutableHashSet<string> variableNames) | ||
| { | ||
| return designation switch | ||
| { | ||
| SingleVariableDesignationSyntax singleVariableDesignation => variableNames.Contains(singleVariableDesignation.Identifier.ValueText), | ||
| ParenthesizedVariableDesignationSyntax parenthesizedVariableDesignation => parenthesizedVariableDesignation.Variables.Any(variable => AnyDeclaredVariablesMatch(variable, variableNames)), | ||
| DiscardDesignationSyntax _ => false, | ||
| _ => false | ||
| }; | ||
| } | ||
|
|
||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
src/Common/CSharp/Analysis/ReduceIfNesting/IfLocalVariableAnalysis.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| // Copyright (c) Josef Pihrt and Contributors. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
|
||
| using System.Collections.Generic; | ||
| using System.Diagnostics; | ||
| using System.Linq; | ||
| using Microsoft.CodeAnalysis; | ||
| using Microsoft.CodeAnalysis.CSharp; | ||
| using Microsoft.CodeAnalysis.CSharp.Syntax; | ||
|
|
||
| namespace Roslynator.CSharp.Analysis.ReduceIfNesting; | ||
|
|
||
| internal static class IfStatementLocalVariableAnalysis | ||
| { | ||
| public static bool DoDeclaredVariablesOverlapWithOuterScope( | ||
| IfStatementSyntax ifStatement, | ||
| SemanticModel semanticModel | ||
| ) | ||
| { | ||
|
|
||
| var ifVariablesDeclared = semanticModel.AnalyzeDataFlow(ifStatement)! | ||
| .VariablesDeclared; | ||
|
|
||
| if (ifVariablesDeclared.IsEmpty) | ||
| return false; | ||
|
|
||
| var outerScope = GetOuterScope(ifStatement); | ||
|
|
||
| if (outerScope == null) | ||
| return true; | ||
|
|
||
| IList<ISymbol> parentVariablesDeclared; | ||
| if (outerScope is SwitchSectionSyntax switchSection) | ||
| { | ||
| parentVariablesDeclared = switchSection.Statements.SelectMany(s => semanticModel.AnalyzeDataFlow(s)!.VariablesDeclared).ToList(); | ||
|
josefpihrt marked this conversation as resolved.
Outdated
|
||
| } | ||
| else | ||
| { | ||
| parentVariablesDeclared = semanticModel.AnalyzeDataFlow(outerScope)!.VariablesDeclared; | ||
| } | ||
|
|
||
|
jamesHargreaves12 marked this conversation as resolved.
Outdated
|
||
| Debug.Assert(parentVariablesDeclared.Count >= ifVariablesDeclared.Length); | ||
| // The parent's declared variables will include those from the if and so we have to check for any symbols occurring twice. | ||
| foreach (var variable in ifVariablesDeclared) | ||
| { | ||
| if (parentVariablesDeclared.Count(s => s.Name == variable.Name) > 1) | ||
| return true; | ||
| } | ||
|
|
||
| return false; | ||
| } | ||
|
|
||
| private static SyntaxNode GetOuterScope(IfStatementSyntax ifStatement) | ||
| { | ||
| return ifStatement.Parent switch | ||
| { | ||
| BlockSyntax block => block, | ||
| ForStatementSyntax forStatement => forStatement.Statement, | ||
| CommonForEachStatementSyntax forEachStatement => forEachStatement.Statement, | ||
| DoStatementSyntax doStatement => doStatement.Statement, | ||
| WhileStatementSyntax whileStatement => whileStatement.Statement, | ||
| SwitchSectionSyntax switchSection => switchSection, | ||
| ConstructorDeclarationSyntax constructorDeclaration => constructorDeclaration.Body, | ||
| DestructorDeclarationSyntax destructorDeclaration => destructorDeclaration.Body, | ||
| AccessorDeclarationSyntax accessorDeclaration => accessorDeclaration.Body, | ||
| OperatorDeclarationSyntax operatorDeclaration => operatorDeclaration.Body, | ||
| ConversionOperatorDeclarationSyntax conversionOperatorDeclaration => conversionOperatorDeclaration.Body, | ||
| MethodDeclarationSyntax methodDeclaration => methodDeclaration.Body, | ||
| LocalFunctionStatementSyntax localFunctionStatement => localFunctionStatement.Body, | ||
| AnonymousFunctionExpressionSyntax anonymousFunctionExpression => anonymousFunctionExpression.Block, | ||
| _ => null | ||
| }; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.