Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit0212991

Browse files
committed
Add minimal binary pattern support for null checks in DFA
1 parent3587540 commit0212991

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed

‎src/NetAnalyzers/UnitTests/Microsoft.CodeQuality.Analyzers/QualityGuidelines/ValidateArgumentsOfPublicMethodsTests.cs‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6864,5 +6864,31 @@ End Class
68646864
},
68656865
}.RunAsync();
68666866
}
6867+
6868+
[Fact,WorkItem(6755,"https://github.com/dotnet/roslyn-analyzers/issues/6755")]
6869+
publicasyncTaskCSharp_BinaryPattern_HandlesNull()
6870+
{
6871+
varcode="""
6872+
#nullable enable
6873+
6874+
public class C
6875+
{
6876+
public string? M(string[]? obj)
6877+
{
6878+
if (obj is null or [])
6879+
{
6880+
return null;
6881+
}
6882+
return obj.ToString(); // error diagnosed here
6883+
}
6884+
}
6885+
""";
6886+
awaitnewVerifyCS.Test
6887+
{
6888+
TestCode=code,
6889+
FixedCode=code,
6890+
LanguageVersion=CodeAnalysis.CSharp.LanguageVersion.CSharp11,
6891+
}.RunAsync();
6892+
}
68676893
}
68686894
}

‎src/Utilities/FlowAnalysis/FlowAnalysis/Framework/DataFlow/DataFlowOperationVisitor.cs‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1612,6 +1612,17 @@ private void PerformPredicateAnalysisCore(IOperation operation, TAnalysisData ta
16121612
caseOperationKind.BinaryPattern:
16131613
// These high level patterns should not be present in the lowered CFG: https://github.com/dotnet/roslyn/issues/47068
16141614
predicateValueKind=PredicateValueKind.Unknown;
1615+
1616+
// We special case common null check to reduce false positives. But this implementation for BinaryPattern is very incomplete.
1617+
if(FlowBranchConditionKind==ControlFlowConditionKind.WhenFalse)
1618+
{
1619+
varbinaryPattern=(IBinaryPatternOperation)isPatternOperation.Pattern;
1620+
if(binaryPattern.OperatorKind==BinaryOperatorKind.Or&&HasNullCheck(binaryPattern))
1621+
{
1622+
predicateValueKind=SetValueForIsNullComparisonOperator(isPatternOperation.Value,equals:false,targetAnalysisData:targetAnalysisData);
1623+
}
1624+
}
1625+
16151626
break;
16161627

16171628
default:
@@ -1761,6 +1772,21 @@ bool IsOverrideOrImplementationOfEquatableEquals(IMethodSymbol methodSymbol)
17611772

17621773
returnfalse;
17631774
}
1775+
1776+
boolHasNullCheck(IOperationoperation)
1777+
{
1778+
if(operationisIConstantPatternOperationconstant&&constant.Value.ConstantValue.HasValue&&constant.Value.ConstantValue.Valueisnull)
1779+
{
1780+
returntrue;
1781+
}
1782+
1783+
if(operationisIBinaryPatternOperation{OperatorKind:BinaryOperatorKind.Or}binaryOrOperation)
1784+
{
1785+
returnHasNullCheck(binaryOrOperation.LeftPattern)||HasNullCheck(binaryOrOperation.RightPattern);
1786+
}
1787+
1788+
returnfalse;
1789+
}
17641790
}
17651791

17661792
protectedvirtualvoidSetPredicateValueKind(IOperationoperation,TAnalysisDataanalysisData,PredicateValueKindpredicateValueKind)

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp