From 7e94d3d334188b1d6fe82d9c09ea3df5d73b00b1 Mon Sep 17 00:00:00 2001 From: James Hargreaves Date: Tue, 25 Jul 2023 18:26:22 +0100 Subject: [PATCH] Fix/rcs1208 incorrect recursive behaviour (#1119) --- ChangeLog.md | 1 + .../CodeFixes/IfStatementCodeFixProvider.cs | 2 +- .../RCS1208ReduceIfNestingTests.cs | 72 ++++++++++++++++--- 3 files changed, 66 insertions(+), 9 deletions(-) diff --git a/ChangeLog.md b/ChangeLog.md index 16fb21326c..cd890ab8bd 100644 --- a/ChangeLog.md +++ b/ChangeLog.md @@ -33,6 +33,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Fix [RCS0005](https://josefpihrt.github.io/docs/roslynator/analyzers/RCS0005) ([#1114](https://github.com/JosefPihrt/Roslynator/pull/1114)). - Fix [RCS1176](https://github.com/JosefPihrt/Roslynator/blob/main/docs/analyzers/RCS1176.md) ([#1122](https://github.com/JosefPihrt/Roslynator/pull/1122)). - Fix [RCS1085](https://github.com/JosefPihrt/Roslynator/blob/main/docs/analyzers/RCS1085.md) ([#1120](https://github.com/josefpihrt/roslynator/pull/1120)). +- Fix [RCS1208](https://github.com/JosefPihrt/Roslynator/blob/main/docs/analyzers/RCS1208.md) ([#1119](https://github.com/JosefPihrt/Roslynator/pull/1119)). ## [4.3.0] - 2023-04-24 diff --git a/src/Analyzers.CodeFixes/CSharp/CodeFixes/IfStatementCodeFixProvider.cs b/src/Analyzers.CodeFixes/CSharp/CodeFixes/IfStatementCodeFixProvider.cs index 70320b2b05..43c0b29c9f 100644 --- a/src/Analyzers.CodeFixes/CSharp/CodeFixes/IfStatementCodeFixProvider.cs +++ b/src/Analyzers.CodeFixes/CSharp/CodeFixes/IfStatementCodeFixProvider.cs @@ -93,7 +93,7 @@ public override async Task RegisterCodeFixesAsync(CodeFixContext context) context.Document, ifStatement, (SyntaxKind)Enum.Parse(typeof(SyntaxKind), diagnostic.Properties["JumpKind"]), - recursive: true, + recursive: false, cancellationToken: ct); }, GetEquivalenceKey(diagnostic)); diff --git a/src/Tests/Analyzers.Tests/RCS1208ReduceIfNestingTests.cs b/src/Tests/Analyzers.Tests/RCS1208ReduceIfNestingTests.cs index e10929fed9..54a9e8298e 100644 --- a/src/Tests/Analyzers.Tests/RCS1208ReduceIfNestingTests.cs +++ b/src/Tests/Analyzers.Tests/RCS1208ReduceIfNestingTests.cs @@ -147,12 +147,12 @@ class C void M(bool p) { var f = () => - { - [|if|] (p) { - M2(); - } - }; + [|if|] (p) + { + M2(); + } + }; } void M2() @@ -172,8 +172,7 @@ void M(bool p) } M2(); - } -; + }; } void M2() @@ -197,7 +196,6 @@ void M3() { M2(); } - } M3(); } @@ -558,6 +556,64 @@ void M2() { } } +"); + } + + [Fact, Trait(Traits.Analyzer, DiagnosticIdentifiers.ReduceIfNesting)] + public async Task TestDiagnostic_DoesNot_IncorrectlyRecurse() + { + await VerifyDiagnosticAndFixAsync(@" +class C +{ + void M(bool p, bool q, bool r) + { + [|if|] (r) + { + if (p) + { + var x = 1; + M2(); + } + + if (q) + { + var x = 2; + M2(); + } + } + } + + void M2() + { + } +} +", @" +class C +{ + void M(bool p, bool q, bool r) + { + if (!r) + { + return; + } + + if (p) + { + var x = 1; + M2(); + } + + if (q) + { + var x = 2; + M2(); + } + } + + void M2() + { + } +} "); } }