From 5c249bb9c93bb1e47868bd783eaca2a7a2ce8a2e Mon Sep 17 00:00:00 2001 From: Pavel Mikula <57188685+pavel-mikula-sonarsource@users.noreply.github.com> Date: Wed, 24 Apr 2024 10:15:43 +0200 Subject: [PATCH] New Rule T0036: Do not activate a nullable context (#9171) --- .../Rules/AvoidNullable.cs | 37 +++++++++++++++++++ .../Rules/AvoidNullableTest.cs | 29 +++++++++++++++ .../TestCases/AvoidNullable.cs | 10 +++++ 3 files changed, 76 insertions(+) create mode 100644 analyzers/src/SonarAnalyzer.CSharp.Styling/Rules/AvoidNullable.cs create mode 100644 analyzers/tests/SonarAnalyzer.CSharp.Styling.Test/Rules/AvoidNullableTest.cs create mode 100644 analyzers/tests/SonarAnalyzer.CSharp.Styling.Test/TestCases/AvoidNullable.cs diff --git a/analyzers/src/SonarAnalyzer.CSharp.Styling/Rules/AvoidNullable.cs b/analyzers/src/SonarAnalyzer.CSharp.Styling/Rules/AvoidNullable.cs new file mode 100644 index 00000000000..8ef9889c6a7 --- /dev/null +++ b/analyzers/src/SonarAnalyzer.CSharp.Styling/Rules/AvoidNullable.cs @@ -0,0 +1,37 @@ +/* + * SonarAnalyzer for .NET + * Copyright (C) 2015-2024 SonarSource SA + * mailto: contact AT sonarsource DOT com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +namespace SonarAnalyzer.Rules.CSharp.Styling; + +[DiagnosticAnalyzer(LanguageNames.CSharp)] +public sealed class AvoidNullable : StylingAnalyzer +{ + public AvoidNullable() : base("T0036", "Do not use nullable context.") { } + + protected override void Initialize(SonarAnalysisContext context) => + context.RegisterNodeAction(c => + { + if (((NullableDirectiveTriviaSyntax)c.Node).SettingToken.IsKind(SyntaxKind.EnableKeyword)) + { + c.ReportIssue(Rule, c.Node); + } + }, + SyntaxKind.NullableDirectiveTrivia); +} diff --git a/analyzers/tests/SonarAnalyzer.CSharp.Styling.Test/Rules/AvoidNullableTest.cs b/analyzers/tests/SonarAnalyzer.CSharp.Styling.Test/Rules/AvoidNullableTest.cs new file mode 100644 index 00000000000..36b26684abc --- /dev/null +++ b/analyzers/tests/SonarAnalyzer.CSharp.Styling.Test/Rules/AvoidNullableTest.cs @@ -0,0 +1,29 @@ +/* + * SonarAnalyzer for .NET + * Copyright (C) 2015-2024 SonarSource SA + * mailto: contact AT sonarsource DOT com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +namespace SonarAnalyzer.CSharp.Styling.Test.Rules; + +[TestClass] +public class AvoidNullableTest +{ + [TestMethod] + public void AvoidNullable() => + StylingVerifierBuilder.Create().AddPaths("AvoidNullable.cs").Verify(); +} diff --git a/analyzers/tests/SonarAnalyzer.CSharp.Styling.Test/TestCases/AvoidNullable.cs b/analyzers/tests/SonarAnalyzer.CSharp.Styling.Test/TestCases/AvoidNullable.cs new file mode 100644 index 00000000000..b977cdc4957 --- /dev/null +++ b/analyzers/tests/SonarAnalyzer.CSharp.Styling.Test/TestCases/AvoidNullable.cs @@ -0,0 +1,10 @@ + +// This is not expected to raise when the whole project is configured with nullable context. + +// Noncompliant@+1 {{Do not use nullable context.}} + #nullable enable +//^^^^^^^^^^^^^^^^ + +#nullable disable // Compliant + +#nullable restore // Compliant