This repository was archived by the owner on Nov 12, 2026. It is now read-only.
Repository navigation
Fix CA1508 false positive for pattern expressions - #6805
Merged
Merged
Conversation
Fixes dotnet#6048 `goto case DiscardPattern` had the required logic for setting the predicated value the pattern value in true branch of is expression, but it was also setting `predicateValueKind`, which is in turn used to identify redundant duplicate conditional checks. Latter is fixed by this change.
mavasani
commented
Jul 26, 2023
Comment on lines
+1569
to
+1572
| if (FlowBranchConditionKind == ControlFlowConditionKind.WhenTrue) | ||
| { | ||
| _ = SetValueForIsNullComparisonOperator(isPatternOperation.Value, equals: false, targetAnalysisData: targetAnalysisData); | ||
| } |
Author
There was a problem hiding this comment.
Added code is just the copy of code in the case OperationKind.DiscardPattern section right below this one, except that we ignore the returned value by SetValueForIsNullComparisonOperator
Author
Codecov Report
Additional details and impacted files@@ Coverage Diff @@
## main #6805 +/- ##
=======================================
Coverage 96.35% 96.35%
=======================================
Files 1386 1386
Lines 326470 326515 +45
Branches 10736 10737 +1
=======================================
+ Hits 314558 314605 +47
+ Misses 9212 9210 -2
Partials 2700 2700 |
Youssef1313
approved these changes
Jul 26, 2023
Youssef1313
reviewed
Jul 26, 2023
| } | ||
| catch (Exception e) | ||
| { | ||
| if (e is ArgumentException ae) |
Member
There was a problem hiding this comment.
Maybe add a test with catch (ArgumentException) and asserting that the condition is flagged as "always true"?
Author
There was a problem hiding this comment.
Sure, let me add it in the next PR.
Author
There was a problem hiding this comment.
@Youssef1313 It seems this is a false negative right now. I'll file a tracking issue for it
using System;
class Test
{
public static void M()
{
try
{
throw new Random().NextDouble() > 0.5 ? new Exception() : new ArgumentException();
}
catch (ArgumentException e)
{
if (e is ArgumentException ae) // This should fire CA1508
Console.Out.WriteLine(""ArgumentException"");
throw;
}
}
}
amiru3f
pushed a commit
to amiru3f/roslyn-analyzers
that referenced
this pull request
Jul 26, 2023
Fix CA1508 false positive for pattern expressions
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
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
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.
Fixes #6048
goto case DiscardPatternhad the required logic for setting the predicated value for the pattern value in true branch of theis expression, but it was also settingpredicateValueKind, which is in turn used to identify redundant duplicate conditional checks. Latter is fixed by this change.