-
Notifications
You must be signed in to change notification settings - Fork 222
/
RedundantCast.CSharp8.cs
74 lines (68 loc) · 2.18 KB
/
RedundantCast.CSharp8.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
using System;
using System.Collections.Generic;
using System.Linq;
namespace Tests.Diagnostics
{
// https://github.com/SonarSource/sonar-dotnet/issues/3273
public class CastOnNullable
{
#nullable enable
public void Simple()
{
var nullable = (string?)"Test"; // Compliant
var nonNullable = (string)nullable!; // Compliant
if (nullable != null)
{
var s1 = (string)nullable; // Compliant
var s2 = (string?)nullable; // Compliant
}
if (nonNullable != null)
{
var s1 = (string)nonNullable; // Compliant
var s2 = (string?)nonNullable; // Compliant
}
}
public void NonNullable()
{
#nullable disable
var s1 = (string?)"Test"; // Compliant
var s2 = (string)s1!; // Compliant
#nullable enable
}
public static IEnumerable<string> UsefulCast()
{
var nullableStrings = new string?[] { "one", "two", null, "three" };
return nullableStrings.OfType<string>(); // Compliant - filters out the null
}
}
// https://github.com/SonarSource/sonar-dotnet/issues/6438
public class AnonTypes
{
public void Simple()
{
var anon = new { X = (string?)"foo" }; // Compliant
}
public void Array()
{
var anonArray = new[] { new { X = (string?)"foo" }, new { X = (string?)null } }; // Compliant
var oneElementAnonArray = new[] { new { X = (string?)"foo" } }; // Compliant
var notSoAnonArray = new[] { new HoldsObject(new { X = (string?)"foo" }) }; // Compliant
}
public void SwitchExpression()
{
var anonSwitch = true switch
{
true => new { X = (string?)"foo" }, // Compliant
false => new { X = (string?)null } // Compliant
};
}
}
internal class HoldsObject
{
object O { get; }
public HoldsObject(object o)
{
O = o;
}
}
}