From 0a5e0304be41923287e65167eb8c8f77c8eaa9f2 Mon Sep 17 00:00:00 2001 From: Tim Pohlmann Date: Thu, 5 Oct 2023 09:59:51 +0200 Subject: [PATCH] Add reproducer for #8149 --- .../ConditionEvaluatesToConstant.CSharp8.cs | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/analyzers/tests/SonarAnalyzer.UnitTest/TestCases/SymbolicExecution/Roslyn/ConditionEvaluatesToConstant.CSharp8.cs b/analyzers/tests/SonarAnalyzer.UnitTest/TestCases/SymbolicExecution/Roslyn/ConditionEvaluatesToConstant.CSharp8.cs index 79f9fd3a80b..ed3bb3d7a77 100644 --- a/analyzers/tests/SonarAnalyzer.UnitTest/TestCases/SymbolicExecution/Roslyn/ConditionEvaluatesToConstant.CSharp8.cs +++ b/analyzers/tests/SonarAnalyzer.UnitTest/TestCases/SymbolicExecution/Roslyn/ConditionEvaluatesToConstant.CSharp8.cs @@ -510,3 +510,34 @@ public void TestMethod() private void Callback() { } } + +// https://github.com/SonarSource/sonar-dotnet/issues/8149 +public class Repro_8149 +{ + enum UserType + { + Internal, + External, + Other + } + + interface IUser + { + int Id { get; } + string LoginName { get; } + UserType UserType { get; } + } + + static void Check_SwitchExpression(IUser user) + { + if (user.UserType switch // Noncompliant FP, always false + { + UserType.Internal => user.Id == 1, + UserType.External => user.LoginName == "test", + _ => false, + }) + return; + + throw new ApplicationException("not authorized"); + } +}