From be9928b5e2f8acf459825612b34c350f6ffb67b2 Mon Sep 17 00:00:00 2001 From: Josef Pihrt Date: Wed, 8 May 2024 19:55:34 +0200 Subject: [PATCH 1/5] update --- .../RCS1208ReduceIfNestingTests.cs | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/Tests/Analyzers.Tests/RCS1208ReduceIfNestingTests.cs b/src/Tests/Analyzers.Tests/RCS1208ReduceIfNestingTests.cs index ef0f749ead..6acb41d420 100644 --- a/src/Tests/Analyzers.Tests/RCS1208ReduceIfNestingTests.cs +++ b/src/Tests/Analyzers.Tests/RCS1208ReduceIfNestingTests.cs @@ -742,4 +742,21 @@ void M2() } "); } + + [Fact, Trait(Traits.Analyzer, DiagnosticIdentifiers.ReduceIfNesting)] + public async Task TestNoDiagnostic() + { + await VerifyNoDiagnosticAsync(""" +class C +{ + private void Foo(string bar, int baz) + { + if (bar == "bar" && baz == 123) + { + var foo = "baz"; + } + } +} +"""); + } } From 471a397f1293426b21bf7b793254ebd6a8503f8f Mon Sep 17 00:00:00 2001 From: Josef Pihrt Date: Wed, 8 May 2024 20:37:13 +0200 Subject: [PATCH 2/5] update --- .../Analysis/ReduceIfNesting/ReduceIfNestingAnalysis.cs | 5 +++-- .../Analysis/ReduceIfNesting/ReduceIfNestingOptions.cs | 1 + .../CSharp/Refactorings/IfStatementRefactoring.cs | 3 ++- 3 files changed, 6 insertions(+), 3 deletions(-) diff --git a/src/Common/CSharp/Analysis/ReduceIfNesting/ReduceIfNestingAnalysis.cs b/src/Common/CSharp/Analysis/ReduceIfNesting/ReduceIfNestingAnalysis.cs index b05b7da339..f25c5e0e64 100644 --- a/src/Common/CSharp/Analysis/ReduceIfNesting/ReduceIfNestingAnalysis.cs +++ b/src/Common/CSharp/Analysis/ReduceIfNesting/ReduceIfNestingAnalysis.cs @@ -113,7 +113,7 @@ private static ReduceIfNestingAnalysisResult AnalyzeCore( return Success(jumpKind, parent); } - if (!IsFixable(ifStatement, statements, ref jumpKind)) + if (!IsFixable(ifStatement, statements, options, ref jumpKind)) return Fail(node); switch (parentKind) @@ -314,6 +314,7 @@ private static bool IsNestedFix(SyntaxNode node, SemanticModel semanticModel, Re private static bool IsFixable( IfStatementSyntax ifStatement, SyntaxList statements, + ReduceIfNestingOptions options, ref SyntaxKind jumpKind) { int i = statements.Count - 1; @@ -326,7 +327,7 @@ private static bool IsFixable( if (statements[i] == ifStatement) { - return true; + return (options & ReduceIfNestingOptions.AllowLastIf) != 0; } else if (IsFixableJumpStatement(statements[i], ref jumpKind)) { diff --git a/src/Common/CSharp/Analysis/ReduceIfNesting/ReduceIfNestingOptions.cs b/src/Common/CSharp/Analysis/ReduceIfNesting/ReduceIfNestingOptions.cs index 9c0209dde1..bb63832ee5 100644 --- a/src/Common/CSharp/Analysis/ReduceIfNesting/ReduceIfNestingOptions.cs +++ b/src/Common/CSharp/Analysis/ReduceIfNesting/ReduceIfNestingOptions.cs @@ -12,4 +12,5 @@ internal enum ReduceIfNestingOptions AllowIfInsideIfElse = 1 << 1, AllowLoop = 1 << 2, AllowSwitchSection = 1 << 3, + AllowLastIf = 1 << 4, } diff --git a/src/Refactorings/CSharp/Refactorings/IfStatementRefactoring.cs b/src/Refactorings/CSharp/Refactorings/IfStatementRefactoring.cs index 0dc3894886..81cabe56f5 100644 --- a/src/Refactorings/CSharp/Refactorings/IfStatementRefactoring.cs +++ b/src/Refactorings/CSharp/Refactorings/IfStatementRefactoring.cs @@ -137,7 +137,8 @@ public static async Task ComputeRefactoringsAsync(RefactoringContext context, If options: ReduceIfNestingOptions.AllowNestedFix | ReduceIfNestingOptions.AllowIfInsideIfElse | ReduceIfNestingOptions.AllowLoop - | ReduceIfNestingOptions.AllowSwitchSection, + | ReduceIfNestingOptions.AllowSwitchSection + | ReduceIfNestingOptions.AllowLastIf, cancellationToken: context.CancellationToken); if (analysis.Success) From 55dd5fd67b1c708d51e51457f224a9d2da56b509 Mon Sep 17 00:00:00 2001 From: Josef Pihrt Date: Wed, 8 May 2024 20:38:49 +0200 Subject: [PATCH 3/5] update --- ChangeLog.md | 1 + 1 file changed, 1 insertion(+) diff --git a/ChangeLog.md b/ChangeLog.md index 0f8f0a1952..6489de7152 100644 --- a/ChangeLog.md +++ b/ChangeLog.md @@ -14,6 +14,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - [CLI] Fix loading of `slnf` files ([PR](https://github.com/dotnet/roslynator/pull/1447)) - [CLI] Fix `--severity-level` ([PR](https://github.com/dotnet/roslynator/pull/1449)) - Fix analyzer [RCS1246](https://josefpihrt.github.io/docs/roslynator/analyzers/RCS1246) ([PR](https://github.com/dotnet/roslynator/pull/1451)) +- Fix analyzer [RCS1208](https://josefpihrt.github.io/docs/roslynator/analyzers/RCS1208) ([PR](https://github.com/dotnet/roslynator/pull/1462)) ## [4.12.1] - 2024-04-15 From 40bb81c2649f2bf4723f3f601c390150e03202a2 Mon Sep 17 00:00:00 2001 From: Josef Pihrt Date: Wed, 8 May 2024 22:15:28 +0200 Subject: [PATCH 4/5] update --- .../RCS1208ReduceIfNestingTests.cs | 223 ++---------------- 1 file changed, 20 insertions(+), 203 deletions(-) diff --git a/src/Tests/Analyzers.Tests/RCS1208ReduceIfNestingTests.cs b/src/Tests/Analyzers.Tests/RCS1208ReduceIfNestingTests.cs index 6acb41d420..46f04287e9 100644 --- a/src/Tests/Analyzers.Tests/RCS1208ReduceIfNestingTests.cs +++ b/src/Tests/Analyzers.Tests/RCS1208ReduceIfNestingTests.cs @@ -15,12 +15,12 @@ public class RCS1208ReduceIfNestingTests : AbstractCSharpDiagnosticVerifier { - [|if|] (p) + if (p) { M2(); } @@ -209,40 +169,20 @@ void M2() { } } -", @" -class C -{ - void M(bool p) - { - var f = () => - { - if (!p) - { - return; - } - - M2(); - }; - } - - void M2() - { - } -} "); } [Fact, Trait(Traits.Analyzer, DiagnosticIdentifiers.ReduceIfNesting)] public async Task Test_WhenParentIsLocalFunction() { - await VerifyDiagnosticAndFixAsync(@" + await VerifyNoDiagnosticAsync(@" class C { void M(bool p) { void M3() { - [|if|] (p) + if (p) { M2(); } @@ -254,39 +194,18 @@ void M2() { } } -", @" -class C -{ - void M(bool p) - { - void M3() - { - if (!p) - { - return; - } - - M2(); - } - M3(); - } - - void M2() - { - } -} "); } [Fact, Trait(Traits.Analyzer, DiagnosticIdentifiers.ReduceIfNesting)] public async Task Test_WhenParentIsMethod() { - await VerifyDiagnosticAndFixAsync(@" + await VerifyNoDiagnosticAsync(@" class C { void M(bool p) { - [|if|] (p) + if (p) { M2(); } @@ -296,23 +215,6 @@ void M2() { } } -", @" -class C -{ - void M(bool p) - { - if (!p) - { - return; - } - - M2(); - } - - void M2() - { - } -} "); } @@ -346,7 +248,7 @@ void M2() [Fact, Trait(Traits.Analyzer, DiagnosticIdentifiers.ReduceIfNesting)] public async Task Test_InvertingCoalesceToFalse() { - await VerifyDiagnosticAndFixAsync(@" + await VerifyNoDiagnosticAsync(@" class C { void M(bool? p) @@ -361,35 +263,18 @@ void M2() { } } -", @" -class C -{ - void M(bool? p) - { - if (p != true) - { - return; - } - - M2(); - } - - void M2() - { - } -} "); } [Fact, Trait(Traits.Analyzer, DiagnosticIdentifiers.ReduceIfNesting)] public async Task Test_InvertingCoalesceToTrue() { - await VerifyDiagnosticAndFixAsync(@" + await VerifyNoDiagnosticAsync(@" class C { void M(bool? p) { - [|if|] (p??true) + if (p??true) { M2(); } @@ -399,36 +284,19 @@ void M2() { } } -", @" -class C -{ - void M(bool? p) - { - if (p == false) - { - return; - } - - M2(); - } - - void M2() - { - } -} "); } [Fact, Trait(Traits.Analyzer, DiagnosticIdentifiers.ReduceIfNesting)] public async Task Test_InvertingCoalesceToUnknown() { - await VerifyDiagnosticAndFixAsync(@" + await VerifyNoDiagnosticAsync(@" class C { bool b { get; set; } void M(bool? p) { - [|if|] (p??b) + if (p??b) { M2(); } @@ -437,23 +305,6 @@ void M2() { } } -", @" -class C -{ - bool b { get; set; } - void M(bool? p) - { - if (!(p ?? b)) - { - return; - } - - M2(); - } - void M2() - { - } -} "); } @@ -668,14 +519,14 @@ void M2() } [Fact, Trait(Traits.Analyzer, DiagnosticIdentifiers.ReduceIfNesting)] - public async Task Test_WhenIsExpressionCsharp8() + public async Task Test_WhenIsExpressionCSharp8() { - await VerifyDiagnosticAndFixAsync(@" + await VerifyNoDiagnosticAsync(@" class C { void M(object o) { - [|if|] (o is string) + if (o is string) { M2(); } @@ -685,35 +536,18 @@ void M2() { } } -", @" -class C -{ - void M(object o) - { - if (!(o is string)) - { - return; - } - - M2(); - } - - void M2() - { - } -} ", options: WellKnownCSharpTestOptions.Default_CSharp8); } [Fact, Trait(Traits.Analyzer, DiagnosticIdentifiers.ReduceIfNesting)] public async Task Test_WhenIsExpression() { - await VerifyDiagnosticAndFixAsync(@" + await VerifyNoDiagnosticAsync(@" class C { void M(object o) { - [|if|] (o is string) + if (o is string) { M2(); } @@ -723,23 +557,6 @@ void M2() { } } -", @" -class C -{ - void M(object o) - { - if (o is not string) - { - return; - } - - M2(); - } - - void M2() - { - } -} "); } From 7e2adb90ecf3858a86545977e119669e7075da51 Mon Sep 17 00:00:00 2001 From: Josef Pihrt Date: Wed, 8 May 2024 22:43:02 +0200 Subject: [PATCH 5/5] update --- src/Tests/Analyzers.Tests/RCS1208ReduceIfNestingTests.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Tests/Analyzers.Tests/RCS1208ReduceIfNestingTests.cs b/src/Tests/Analyzers.Tests/RCS1208ReduceIfNestingTests.cs index 46f04287e9..4ebe2107aa 100644 --- a/src/Tests/Analyzers.Tests/RCS1208ReduceIfNestingTests.cs +++ b/src/Tests/Analyzers.Tests/RCS1208ReduceIfNestingTests.cs @@ -253,7 +253,7 @@ class C { void M(bool? p) { - [|if|] (p??false) + if (p??false) { M2(); }