-
-
Notifications
You must be signed in to change notification settings - Fork 261
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix/RCS1031, RCS1208 and RCS1061 (#1062)
Co-authored-by: Josef Pihrt <[email protected]>
- Loading branch information
1 parent
e017499
commit 3b534ee
Showing
11 changed files
with
823 additions
and
61 deletions.
There are no files selected for viewing
This file contains 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 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
38 changes: 38 additions & 0 deletions
38
...lyzers/CSharp/Analysis/PattenMatchingAnalysis/PatternMatchingVariableDeclarationHelper.cs
This file contains 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,38 @@ | ||
// 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 PatternMatchingVariableDeclarationHelper | ||
{ | ||
public static bool AnyDeclaredVariablesMatch(PatternSyntax pattern, ImmutableHashSet<string> variableNames) | ||
{ | ||
return pattern switch | ||
{ | ||
RecursivePatternSyntax { PositionalPatternClause: var positionalPatternClause, PropertyPatternClause: var propertyPatternClause, Designation: var designation } => | ||
(designation is not null && AnyDeclaredVariablesMatch(designation, variableNames)) | ||
|| (propertyPatternClause?.Subpatterns.Any(p => AnyDeclaredVariablesMatch(p.Pattern, variableNames)) ?? false) | ||
|| (positionalPatternClause?.Subpatterns.Any(p => AnyDeclaredVariablesMatch(p.Pattern, variableNames)) ?? false), | ||
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 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 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
40 changes: 40 additions & 0 deletions
40
src/Common/CSharp/Analysis/ReduceIfNesting/IfLocalVariableAnalysis.cs
This file contains 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,40 @@ | ||
// 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; | ||
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 | ||
) | ||
{ | ||
ImmutableArray<ISymbol> ifVariablesDeclared = semanticModel.AnalyzeDataFlow(ifStatement)! | ||
.VariablesDeclared; | ||
|
||
if (ifVariablesDeclared.IsEmpty) | ||
return false; | ||
|
||
foreach (StatementSyntax statement in SyntaxInfo.StatementListInfo(ifStatement).Statements) | ||
{ | ||
if (statement == ifStatement) | ||
continue; | ||
|
||
foreach (ISymbol parentVariable in semanticModel.AnalyzeDataFlow(statement)!.VariablesDeclared) | ||
{ | ||
foreach (ISymbol ifVariable in ifVariablesDeclared) | ||
{ | ||
if (ifVariable.Name == parentVariable.Name) | ||
return true; | ||
} | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
} |
This file contains 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 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.