Skip to content

Commit

Permalink
New Rule T0036: Do not activate a nullable context (#9171)
Browse files Browse the repository at this point in the history
  • Loading branch information
pavel-mikula-sonarsource authored Apr 24, 2024
1 parent 2536ed3 commit 5c249bb
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 0 deletions.
37 changes: 37 additions & 0 deletions analyzers/src/SonarAnalyzer.CSharp.Styling/Rules/AvoidNullable.cs
Original file line number Diff line number Diff line change
@@ -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);
}
Original file line number Diff line number Diff line change
@@ -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<AvoidNullable>().AddPaths("AvoidNullable.cs").Verify();
}
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 5c249bb

Please sign in to comment.