Skip to content
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

Fix S3431 FP: Don't raise if assertions are done in catch or finally #8300

Closed
martin-strecker-sonarsource opened this issue Nov 2, 2023 · 0 comments · Fixed by #9615
Closed
Assignees
Labels
Area: C# C# rules related issues. Type: False Positive Rule IS triggered when it shouldn't be.
Milestone

Comments

@martin-strecker-sonarsource
Copy link
Contributor

martin-strecker-sonarsource commented Nov 2, 2023

Description

S3431 is raised when [ExpectedException] is used for a test method. Some test methods rely on the logic of uncaught exceptions as the result of the execution. Forcing the developer to use other means than [ExpectedException] can make the test flow difficult to read.

Repro steps

        [TestMethod]
        [ExpectedException(typeof(InvalidOperationException))] // FP. The attribute makes the test method much simpler.
        public void UsingTest()
        {
            try
            {
                Console.ForegroundColor = ConsoleColor.Black;
                using var _ = new ConsoleAlert();
                Assert.AreEqual(ConsoleColor.Red, Console.ForegroundColor);
                throw new InvalidOperationException();
            }
            finally
            {
                Assert.AreEqual(ConsoleColor.Black, Console.ForegroundColor); // We don't care about the exception itself but only about the code flow.
            }
        }

public sealed class ConsoleAlert : IDisposable
{
    private readonly ConsoleColor previous;

    public  ConsoleAlert()
    {
        previous = Console.ForegroundColor;
        Console.ForegroundColor = ConsoleColor.Red;
    }
    
    public void Dispose() =>
        Console.ForegroundColor = previous;
}

Expected behavior

S3431 is not raised.

Actual behavior

S3431 is raised.

Known workarounds

The test code could be moved to a local function and the local function could be asserted to throw. This seems cumbersome, given that there is a simple attribute for this kind of situation. The rule should not raise if assertions are done in catch or finally blocks.

Related information

  • C#/VB.NET Plugins version: latest

See also this issue on SQ and SonarSource/sonar-scanner-msbuild#1737 (comment)

@martin-strecker-sonarsource martin-strecker-sonarsource added Type: False Positive Rule IS triggered when it shouldn't be. Area: C# C# rules related issues. labels Nov 2, 2023
@sebastien-marichal sebastien-marichal self-assigned this Aug 7, 2024
@sebastien-marichal sebastien-marichal changed the title Fix S3431 FP: Don't raise if assertions are done in finally Fix S3431 FP: Don't raise if assertions are done in catch or finally Aug 7, 2024
@zsolt-kolbay-sonarsource zsolt-kolbay-sonarsource added this to the 9.32 milestone Aug 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area: C# C# rules related issues. Type: False Positive Rule IS triggered when it shouldn't be.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants