diff --git a/analyzers/tests/SonarAnalyzer.Test/TestCases/DeadStores.RoslynCfg.cs b/analyzers/tests/SonarAnalyzer.Test/TestCases/DeadStores.RoslynCfg.cs index 507318064f0..263f820bc85 100644 --- a/analyzers/tests/SonarAnalyzer.Test/TestCases/DeadStores.RoslynCfg.cs +++ b/analyzers/tests/SonarAnalyzer.Test/TestCases/DeadStores.RoslynCfg.cs @@ -914,6 +914,59 @@ private void SwitchCase(object sender, DateTimeKind e) } } + class NullConditionalOperatorInTry + { + private void Method(bool condition) + { + var i = 5; // Noncompliant FP + M(i += 1, i += 1); // the first one is an FP, the second a TP + // ^^^^^^ Noncompliant + // ^^^^^^ Noncompliant@-1 + } + + void M(int i, int j) + { + Console.WriteLine(i); + Console.WriteLine(j); + } + } + + class ObjectInitializer + { + public int ID { get; set; } + + void Method() + { + var x = new ObjectInitializer(); // FN + x = new ObjectInitializer { ID = 1 }; + x.Method(); + } + } + + class TernaryInTry + { + void Method(out string param, string param2) + { + var s = ""; // FN + s = param2 switch + { + "a" => "a", + _ => "b" + }; + param = s; + } + } + + class OutParameter + { + void Method(out string param) + { + var s = ""; // FN + Method(out s); + param = s; + } + } + public class ReproGithubIssue697 { private bool DoStuff() => true; diff --git a/analyzers/tests/SonarAnalyzer.Test/TestCases/MethodParameterUnused.RoslynCfg.Fixed.cs b/analyzers/tests/SonarAnalyzer.Test/TestCases/MethodParameterUnused.RoslynCfg.Fixed.cs index 71144716fa2..1795d71fe68 100644 --- a/analyzers/tests/SonarAnalyzer.Test/TestCases/MethodParameterUnused.RoslynCfg.Fixed.cs +++ b/analyzers/tests/SonarAnalyzer.Test/TestCases/MethodParameterUnused.RoslynCfg.Fixed.cs @@ -486,6 +486,43 @@ public void Method(int a } } + public class SwitchInTry + { + private int Method(int i) // Compliant + { + try + { + switch (i) + { + case 0: + return 1; + case 1: + return 2; + default: + return 3; + } + } + catch + { + return 4; + } + } + } + + class NullConditionalOperatorInTry + { + private void Method(string s) // Compliant + { + try + { + s?.ToString(); + } + catch + { + } + } + } + public class ReproGithubIssue2010 { static int PatternMatch(StringSplitOptions splitOptions, int i) diff --git a/analyzers/tests/SonarAnalyzer.Test/TestCases/MethodParameterUnused.RoslynCfg.cs b/analyzers/tests/SonarAnalyzer.Test/TestCases/MethodParameterUnused.RoslynCfg.cs index 0a356ab14c6..a8e66c8ca7a 100644 --- a/analyzers/tests/SonarAnalyzer.Test/TestCases/MethodParameterUnused.RoslynCfg.cs +++ b/analyzers/tests/SonarAnalyzer.Test/TestCases/MethodParameterUnused.RoslynCfg.cs @@ -488,6 +488,43 @@ public void Method(int a, } } + public class SwitchInTry + { + private int Method(int i) // Compliant + { + try + { + switch (i) + { + case 0: + return 1; + case 1: + return 2; + default: + return 3; + } + } + catch + { + return 4; + } + } + } + + class NullConditionalOperatorInTry + { + private void Method(string s) // Compliant + { + try + { + s?.ToString(); + } + catch + { + } + } + } + public class ReproGithubIssue2010 { static int PatternMatch(StringSplitOptions splitOptions, int i) diff --git a/analyzers/tests/SonarAnalyzer.Test/TestCases/SymbolicExecution/Roslyn/NullPointerDereference.cs b/analyzers/tests/SonarAnalyzer.Test/TestCases/SymbolicExecution/Roslyn/NullPointerDereference.cs index cedd41a1996..89019176797 100644 --- a/analyzers/tests/SonarAnalyzer.Test/TestCases/SymbolicExecution/Roslyn/NullPointerDereference.cs +++ b/analyzers/tests/SonarAnalyzer.Test/TestCases/SymbolicExecution/Roslyn/NullPointerDereference.cs @@ -1085,6 +1085,25 @@ static class Extensions public static void MyExtension(this object o) { } } + class ObjectInitializerInTry + { + public int ID { get; set; } + + void Method() + { + ObjectInitializerInTry x = null; + try + { + x = new ObjectInitializerInTry { ID = 1 }; + x.Method(); + } + catch + { + _ = x.ID; // Noncompliant + } + } + } + class Foo // https://github.com/SonarSource/sonar-dotnet/issues/538 { private string bar;