forked from github/codeql
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConstantCondition.cs
More file actions
169 lines (149 loc) · 3.19 KB
/
ConstantCondition.cs
File metadata and controls
169 lines (149 loc) · 3.19 KB
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
using System;
using System.Collections;
using System.Diagnostics;
class ConstantCondition
{
const bool Field = false;
void M1(int x)
{
if (Field) // GOOD: Allow conditional execution based on constant field
;
const bool local = false;
if (local) // GOOD: Allow conditional execution based on local constant
;
try
{
throw new ArgumentNullException("x");
}
finally
{
if (x > 1) // No 'false' successor (instead a 'throw[ArgumentNullException]' successor)
throw new Exception();
}
}
int M2(bool? b) => (b ?? false) ? 0 : 1; // GOOD
bool M3(double d) => d == d; // BAD: but flagged by cs/constant-comparison
}
class ConstantNullness
{
void M1(int i)
{
var j = ((string)null)?.Length; // $ Alert
var s = ((int?)i)?.ToString(); // $ Alert
var k = s?.Length; // GOOD
k = s?.ToLower()?.Length; // GOOD
}
void M2(int i)
{
var j = (int?)null ?? 0; // $ Alert
var s = "" ?? "a"; // $ Alert
j = (int?)i ?? 1; // $ Alert
s = ""?.CommaJoinWith(s); // $ Alert
s = s ?? ""; // GOOD
s = (i == 0 ? s : null) ?? s; // GOOD
var k = (i == 0 ? s : null)?.Length; // GOOD
}
}
class ConstantMatching
{
void M1()
{
switch (1 + 2)
{
case 2: // $ Alert
break;
case 3: // $ Alert
break;
case int _: // GOOD
break;
}
}
void M2(string s)
{
switch ((object)s)
{
case int _: // $ Alert
break;
case "": // GOOD
break;
}
}
void M3(object o)
{
switch (o)
{
case IList _: // GOOD
break;
}
}
string M4(object o)
{
return o switch
{
_ => o.ToString() // $ Alert
};
}
string M5(object o)
{
return o switch
{
"" => " ",
_ => o.ToString() // GOOD
};
}
void M6(bool b1, bool b2)
{
if (!b1)
return;
if (!b2)
return;
if (b1 && b2) // $ Alert
return;
}
string M7(object o)
{
return o switch
{
(string s, _) => s, // GOOD
(_, string s) => s, // GOOD
_ => "" // GOOD
};
}
string M8(int i)
{
return i switch
{
_ when i % 2 == 0 => "even", // GOOD
_ => "odd" // GOOD
};
}
string M9(int i)
{
switch (i)
{
case var _: // $ Alert
return "even";
}
}
string M10(int i)
{
switch (i)
{
case var _ when i % 2 == 0: // GOOD
return "even";
case var _: // GOOD
return "odd";
}
}
}
class Assertions
{
void F()
{
Debug.Assert(false ? false : true); // GOOD
}
}
static class Ext
{
public static string CommaJoinWith(this string s1, string s2) => s1 + ", " + s2;
}