Skip to content
This repository was archived by the owner on Nov 12, 2026. It is now read-only.

Fix CA1508 false positive for pattern expressions - #6805

Merged
mavasani merged 1 commit into
dotnet:mainfrom
mavasani:CA1508_Rethrow
Jul 26, 2023
Merged

mavasani merged 1 commit into
dotnet:mainfrom
mavasani:CA1508_Rethrow

Conversation

@mavasani

Copy link
Copy Markdown

Fixes #6048

goto case DiscardPattern had the required logic for setting the predicated value for the pattern value in true branch of the 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.

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
mavasani requested a review from a team as a code owner July 26, 2023 10:34
Comment on lines +1569 to +1572
if (FlowBranchConditionKind == ControlFlowConditionKind.WhenTrue)
{
_ = SetValueForIsNullComparisonOperator(isPatternOperation.Value, equals: false, targetAnalysisData: targetAnalysisData);
}

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

@mavasani

Copy link
Copy Markdown
Author

@Youssef1313

@codecov

codecov Bot commented Jul 26, 2023

Copy link
Copy Markdown

Codecov Report

Merging #6805 (8887587) into main (65db82e) will increase coverage by 0.00%.
The diff coverage is 100.00%.

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           

}
catch (Exception e)
{
if (e is ArgumentException ae)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe add a test with catch (ArgumentException) and asserting that the condition is flagged as "always true"?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, let me add it in the next PR.

@mavasani mavasani Jul 26, 2023 •

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@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;
        }
    }
}

@mavasani
mavasani merged commit 1121367 into dotnet:main Jul 26, 2023
@mavasani
mavasani deleted the CA1508_Rethrow branch July 26, 2023 11:13
amiru3f pushed a commit to amiru3f/roslyn-analyzers that referenced this pull request Jul 26, 2023
Fix CA1508 false positive for pattern expressions
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

CA1508 - False positive for "is"-operator in catch-block with rethrow

2 participants