-
Notifications
You must be signed in to change notification settings - Fork 222
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New Rule T0012: Use null instead of default for reference types
- Loading branch information
1 parent
0547942
commit 3b9feb8
Showing
5 changed files
with
259 additions
and
6 deletions.
There are no files selected for viewing
44 changes: 44 additions & 0 deletions
44
analyzers/src/SonarAnalyzer.CSharp.Styling/Rules/UseNullInsteadOfDefault.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,44 @@ | ||
/* | ||
* 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 UseNullInsteadOfDefault : StylingAnalyzer | ||
{ | ||
public UseNullInsteadOfDefault() : base("T0012", "Use 'null' instead of 'default' for reference types.") { } | ||
|
||
protected override void Initialize(SonarAnalysisContext context) => | ||
context.RegisterNodeAction( | ||
c => | ||
{ | ||
if (IsReferenceType(c.Node, c.SemanticModel)) | ||
{ | ||
c.ReportIssue(Rule, c.Node); | ||
} | ||
}, | ||
SyntaxKind.DefaultLiteralExpression, SyntaxKind.DefaultExpression); | ||
|
||
private static bool IsReferenceType(SyntaxNode expression, SemanticModel semanticModel) | ||
{ | ||
var typeInfo = semanticModel.GetTypeInfo(expression); | ||
return typeInfo.Type is not IErrorTypeSymbol && ((typeInfo.Type?.IsReferenceType ?? false) || typeInfo.Type.Is(KnownType.System_Nullable_T)); | ||
} | ||
} |
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
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
31 changes: 31 additions & 0 deletions
31
analyzers/tests/SonarAnalyzer.CSharp.Styling.Test/Rules/UseNullInsteadOfDefaultTest.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,31 @@ | ||
/* | ||
* 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 UseNullInsteadOfDefaultTest | ||
{ | ||
private readonly VerifierBuilder builder = StylingVerifierBuilder.Create<UseNullInsteadOfDefault>().WithConcurrentAnalysis(false); | ||
|
||
[TestMethod] | ||
public void UseNullInsteadOfDefault() => | ||
builder.AddPaths("UseNullInsteadOfDefault.cs").Verify(); | ||
} |
181 changes: 181 additions & 0 deletions
181
analyzers/tests/SonarAnalyzer.CSharp.Styling.Test/TestCases/UseNullInsteadOfDefault.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,181 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
|
||
class UseNullInsteadOfDefault | ||
{ | ||
void Method() | ||
{ | ||
int integer = default; | ||
int x = default, y = 0, z = default(int); | ||
var integer2 = default(Int32); | ||
object objectReference = default; // Noncompliant {{Use 'null' instead of 'default' for reference types.}} | ||
// ^^^^^^^ | ||
var objectReference2 = default(object); // Noncompliant {{Use 'null' instead of 'default' for reference types.}} | ||
// ^^^^^^^^^^^^^^^ | ||
object obj1 = default, obj2 = null, obj3 = default(object); | ||
// ^^^^^^^ | ||
// ^^^^^^^^^^^^^^^@-1 | ||
IEnumerable<string> collection = null; | ||
IEnumerable<string> collection2 = default; // Noncompliant | ||
IEnumerable<string> collection3 = true ? null : default; // Noncompliant | ||
collection = null; | ||
collection2 = default; // Noncompliant | ||
collection2 = true ? null : default; // Noncompliant | ||
|
||
Console.WriteLine(default(object) is null); // Noncompliant FP - Do we care? | ||
Console.WriteLine(collection is default(IEnumerable<string>)); // Noncompliant | ||
|
||
|
||
int? nullableInt = default; // Noncompliant | ||
|
||
_ = collection == default; // Noncompliant | ||
|
||
switch (integer) | ||
{ | ||
case default(int): | ||
break; | ||
default: | ||
break; | ||
} | ||
|
||
switch (collection) | ||
{ | ||
case default(IEnumerable<string>): // Noncompliant | ||
break; | ||
default: | ||
break; | ||
} | ||
} | ||
|
||
void GenericMethod<T>() | ||
{ | ||
T t = default; | ||
var t2 = default(T); | ||
T? t3 = default; | ||
T? t4 = default(T); | ||
|
||
t = default; | ||
t = default(T); | ||
t3 = default; | ||
t3 = default(T); | ||
|
||
_ = t == default; // Error [CS8761] | ||
// Error@+1 [CS8761] | ||
_ = t3 == default; | ||
_ = t is default(T); // Error [CS0150] | ||
// Error@+1 [CS0150] | ||
_ = t3 is default(T); | ||
|
||
switch (t) | ||
{ | ||
case default(T): // Error [CS0150] | ||
break; | ||
default: | ||
break; | ||
} | ||
|
||
switch (t3) | ||
{ | ||
// Error@+1 [CS0150] | ||
case default(T): | ||
break; | ||
default: | ||
break; | ||
} | ||
} | ||
|
||
void GenericMethodClassConstraint<T>() | ||
where T : class | ||
{ | ||
T t = default; // Noncompliant | ||
var t2 = default(T); // Noncompliant | ||
T? t3 = default; // Noncompliant | ||
T? t4 = default(T); // Noncompliant | ||
|
||
t = default; // Noncompliant | ||
t = default(T); // Noncompliant | ||
t3 = default; // Noncompliant | ||
t3 = default(T); // Noncompliant | ||
|
||
_ = t == default; // Noncompliant | ||
_ = t3 == default; // Noncompliant | ||
_ = t3 != default; // Noncompliant | ||
_ = t is default(T); // Noncompliant | ||
_ = t3 is default(T); // Noncompliant | ||
_ = t is not default(T); // Noncompliant | ||
_ = t3 is not default(T); // Noncompliant | ||
|
||
switch (t) | ||
{ | ||
case default(T): // Noncompliant | ||
break; | ||
default: | ||
break; | ||
} | ||
|
||
switch (t3) | ||
{ | ||
case default(T): // Noncompliant | ||
break; | ||
default: | ||
break; | ||
} | ||
} | ||
|
||
void GenericMethodStructConstraint<T>() | ||
where T : struct | ||
{ | ||
T t = default; | ||
var t2 = default(T); | ||
|
||
T? t3 = default; // Noncompliant | ||
} | ||
|
||
void GenericMethodEnumConstraint<T>() | ||
where T : Enum | ||
{ | ||
T t = default; | ||
var t2 = default(T); | ||
|
||
T? t3 = default; | ||
} | ||
|
||
void GenericMethodStructEnumConstraint<T>() | ||
where T : struct, Enum | ||
{ | ||
T t = default; | ||
var t2 = default(T); | ||
T? t3 = default; // Noncompliant | ||
T? t4 = default(T); // Noncompliant | ||
|
||
t = default; | ||
t = default(T); | ||
t3 = default; | ||
t3 = default(T); | ||
|
||
_ = t == default; // Error [CS8761] | ||
// Error@+1 [CS0019] | ||
_ = t3 == default; | ||
_ = t is default(T); // Error [CS0150] | ||
// Error@+1 [CS0150] | ||
_ = t3 is default(T); | ||
|
||
switch (t) | ||
{ | ||
// Error@+1 [CS0150] | ||
case default(T): | ||
break; | ||
default: | ||
break; | ||
} | ||
|
||
switch (t3) | ||
{ | ||
// Error@+1 [CS0150] | ||
case default(T): | ||
break; | ||
default: | ||
break; | ||
} | ||
} | ||
} |