-
Notifications
You must be signed in to change notification settings - Fork 226
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New Rule T0036: Do not activate a nullable context (#9171)
- Loading branch information
1 parent
2536ed3
commit 5c249bb
Showing
3 changed files
with
76 additions
and
0 deletions.
There are no files selected for viewing
37 changes: 37 additions & 0 deletions
37
analyzers/src/SonarAnalyzer.CSharp.Styling/Rules/AvoidNullable.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
29 changes: 29 additions & 0 deletions
29
analyzers/tests/SonarAnalyzer.CSharp.Styling.Test/Rules/AvoidNullableTest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
10 changes: 10 additions & 0 deletions
10
analyzers/tests/SonarAnalyzer.CSharp.Styling.Test/TestCases/AvoidNullable.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |