Repository navigation
Expand file tree
/
Copy pathS9395CheckSample.java
More file actions
284 lines (235 loc) · 8.75 KB
/
Copy pathS9395CheckSample.java
File metadata and controls
284 lines (235 loc) · 8.75 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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
package checks;
class S9395CheckSample {
void variableInitialization() {
int intVar = 100_000_000;
long longVar = 100_000_000_000L;
float f1 = intVar; // Noncompliant {{Explicitly cast this "int" to "float" to document potential precision loss.}}
// ^^^^^^
float f2 = longVar; // Noncompliant {{Explicitly cast this "long" to "float" to document potential precision loss.}}
// ^^^^^^^
double d1 = longVar; // Noncompliant {{Explicitly cast this "long" to "double" to document potential precision loss.}}
// ^^^^^^^
double d2 = intVar; // compliant: int -> double is not lossy
float f3 = (float) intVar; // compliant: explicit cast
float f4 = (float) longVar; // compliant: explicit cast
double d3 = (double) longVar; // compliant: explicit cast
byte byteVar = 1;
short shortVar = 1;
char charVar = 'a';
float f5 = byteVar; // compliant: byte fits in float
float f6 = shortVar; // compliant: short fits in float
float f7 = charVar; // compliant: char fits in float
double d4 = byteVar; // compliant: byte fits in double
double d5 = shortVar; // compliant: short fits in double
float f8 = 42; // compliant: small literal within range
float f9 = 16_777_216; // compliant: exactly 2^24, representable
float f10 = 16_777_217; // Noncompliant
// ^^^^^^^^^^
float f11 = 3.14f; // compliant: already float
double d6 = 42L; // compliant: small long literal within range
double d7 = 9_007_199_254_740_992L; // compliant: exactly 2^53, representable
double d8 = 9_007_199_254_740_993L; // Noncompliant
// ^^^^^^^^^^^^^^^^^^^^^^
float f12 = 0; // compliant: zero
long longLit = 100L;
float f13 = longLit; // Noncompliant
float f14 = 100L; // compliant: small long literal
int intVal = 42;
double d9 = intVal; // compliant: int -> double is safe
}
void assignment() {
int intVar = 100_000_000;
long longVar = 100_000_000_000L;
float floatField;
double doubleField;
floatField = intVar; // Noncompliant
// ^^^^^^
floatField = longVar; // Noncompliant
doubleField = longVar; // Noncompliant
doubleField = intVar; // compliant
floatField = (float) intVar; // compliant
}
void compoundAssignment() {
long longVar = 100_000_000_000L;
int intVar = 100;
float floatVar = 1.0f;
double doubleVar = 1.0;
floatVar += intVar; // Noncompliant
// ^^^^^^
floatVar += longVar; // Noncompliant
doubleVar += longVar; // Noncompliant
doubleVar += intVar; // compliant
floatVar += (float) intVar; // compliant
floatVar -= longVar; // Noncompliant
floatVar *= intVar; // Noncompliant
doubleVar -= longVar; // Noncompliant
}
void methodArguments(int intVar, long longVar) {
takeFloat(intVar); // Noncompliant
// ^^^^^^
takeFloat(longVar); // Noncompliant
takeDouble(longVar); // Noncompliant
takeDouble(intVar); // compliant
takeFloat((float) intVar); // compliant
takeFloat(42); // compliant: small literal
}
void takeFloat(float f) {}
void takeDouble(double d) {}
void constructorArguments(int intVar, long longVar) {
new FloatHolder(intVar); // Noncompliant
// ^^^^^^
new FloatHolder(longVar); // Noncompliant
new DoubleHolder(longVar); // Noncompliant
new DoubleHolder(intVar); // compliant
new FloatHolder((float) intVar); // compliant
}
static class FloatHolder {
FloatHolder(float f) {}
}
static class DoubleHolder {
DoubleHolder(double d) {}
}
float returnIntAsFloat(int intVar) {
return intVar; // Noncompliant
// ^^^^^^
}
float returnLongAsFloat(long longVar) {
return longVar; // Noncompliant
}
double returnLongAsDouble(long longVar) {
return longVar; // Noncompliant
}
double returnIntAsDouble(int intVar) {
return intVar; // compliant
}
float returnExplicitCast(int intVar) {
return (float) intVar; // compliant
}
float returnSmallLiteral() {
return 42; // compliant
}
void ternaryExpression(boolean condition, int intA, int intB) {
float f = condition ? intA : intB; // Noncompliant
// ^^^^^^^^^^^^^^^^^^^^^^^
}
void parenthesizedExpression(int intVar) {
float f = (intVar); // Noncompliant
// ^^^^^^
float f2 = ((intVar)); // Noncompliant
// ^^^^^^
}
void multipleParams(int intVar, long longVar) {
takeTwoFloats(intVar, longVar); // Noncompliant 2
// ^^^^^^
}
void takeTwoFloats(float a, float b) {}
void lambdaReturnDoesNotAffect(int intVar) {
Runnable r = () -> {
float f = intVar; // Noncompliant
};
}
void intArithmeticToFloat(int a, int b, long c) {
float f1 = a + b; // compliant: reported by S2184, the fix is to cast an operand
float f2 = a / b; // compliant: reported by S2184, casting the result would not fix the integer division
float f3 = a * b; // compliant: reported by S2184
double d1 = c - a; // compliant: reported by S2184
float f4 = (a / b); // Noncompliant {{Cast one of the operands of this "int" operation to "float" to document potential precision loss.}}
// ^^^^^
float f5 = a % b; // Noncompliant
// ^^^^^
float f6 = (float) a / b; // compliant
}
float field;
void intArithmeticNotReportedByS2184(int a, int b, long c, float fv, double dv) {
float f1 = (a * b); // Noncompliant {{Cast one of the operands of this "int" operation to "float" to document potential precision loss.}}
// ^^^^^
float f2 = ((a + b)); // Noncompliant
// ^^^^^
fv = (a - b); // Noncompliant
// ^^^^^
fv += a * b; // Noncompliant
// ^^^^^
dv -= c + a; // Noncompliant {{Cast one of the operands of this "long" operation to "double" to document potential precision loss.}}
// ^^^^^
fv += a / b; // compliant: reported by S2184
fv *= (a / b); // compliant: reported by S2184
field = a / b; // compliant: reported by S2184
takeFloatVarargs(a * b, "x", "y"); // Noncompliant
// ^^^^^
takeFloatVarargs(a / b); // Noncompliant
// ^^^^^
takeFloatVarargs(a * b, "x"); // compliant: reported by S2184
takeFloat((a * b)); // Noncompliant
// ^^^^^
takeFloat(a * b); // compliant: reported by S2184
}
float returnParenthesizedArithmetic(int a, int b) {
return (a * b); // Noncompliant
// ^^^^^
}
float returnArithmetic(int a, int b) {
return a * b; // compliant: reported by S2184
}
void hexAndBinaryLiterals() {
float f1 = 0xFF; // compliant: 255 is within range
float f2 = 0x1000001; // Noncompliant
// ^^^^^^^^^
float f3 = 0b1; // compliant: 1 is within range
int hexMask = 0xFF000000;
float f4 = hexMask; // Noncompliant
// ^^^^^^^
float f5 = 0xFF000000; // compliant: hex literal value -16777216 is representable in float
}
void negativeLiterals() {
float f1 = -1; // compliant: small negative literal
float f2 = -16_777_216; // compliant: exactly -2^24, representable
float f3 = -16_777_217; // Noncompliant
// ^^^^^^^^^^^
double d1 = -42L; // compliant: small negative long literal
}
static final int SMALL_CONSTANT = 100;
static final int LARGE_CONSTANT = 20_000_000;
void namedConstants() {
float f1 = SMALL_CONSTANT; // compliant: named compile-time constant within range
float f2 = LARGE_CONSTANT; // Noncompliant
// ^^^^^^^^^^^^^^
}
void varargs(int intVar) {
takeFloatVarargs(intVar, "a", "b"); // Noncompliant
// ^^^^^^
takeFloatVarargs(42, "a"); // compliant: small literal
takeFloatVarargs((float) intVar, "a"); // compliant: explicit cast
}
void takeFloatVarargs(float f, Object... args) {}
float returnWithLambdaInside(int intVar) {
Runnable r = () -> {
int x = 1;
};
return intVar; // Noncompliant
}
float returnWithAnonymousClassInside(int intVar) {
Object obj = new Object() {
int getValue() { return intVar; } // compliant: enclosing float return type must not apply here
};
return intVar; // Noncompliant
}
void largeLongLiteralToFloat() {
float f = 20_000_000L; // Noncompliant
// ^^^^^^^^^^^
}
int nonFloatingPointReturn(int intVar) {
return intVar; // compliant: return type is int, not float
}
double returnIntAsDoubleFromLambda() {
java.util.function.IntToDoubleFunction f = (int x) -> {
return x; // compliant: int -> double is not lossy
};
return 0;
}
float returnLongFromLambdaInside(long longVar) {
java.util.function.LongSupplier s = () -> {
return longVar; // compliant: lambda return type is long, not the enclosing float
};
return 0f;
}
}