mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-08-27 15:13:13 +07:00
Even if the return value is not used, it's subject for IgnoreResultOfCallInspection to report it (as we consider only pure methods). It could sometimes be suppressed (e.g., inside assertThrows), in which case it's desired not to report anything. Fixes IDEA-368213 False positive and code-breaking autofix for ObviousNullCheck GitOrigin-RevId: 0ab2fd64b8a8f8599920cffad76fc830790f8269
69 lines
2.2 KiB
Java
69 lines
2.2 KiB
Java
import org.jetbrains.annotations.*;
|
|
import java.util.*;
|
|
|
|
abstract class ObviousNullCheck {
|
|
abstract String getFoo();
|
|
|
|
abstract String getBar();
|
|
|
|
void test(String param) {
|
|
assertNotNull(<warning descr="Redundant null-check: a value of primitive type 'int' is never null">5 + 6</warning>);
|
|
|
|
assertNull("Null!", param);
|
|
assertNull(param, <warning descr="Null-check will always fail: literal is never null">"Null!"</warning>);
|
|
|
|
Objects.requireNonNull(null);
|
|
Objects.requireNonNull(<warning descr="Redundant null-check: literal is never null">"xyz"</warning>, "xyz");
|
|
Objects.requireNonNull((<warning descr="Redundant null-check: concatenation is never null">getFoo() + getBar()</warning>));
|
|
Objects.requireNonNull(<warning descr="Redundant null-check: newly created object is never null">new ArrayList()</warning>, "new returned null");
|
|
Objects.requireNonNull(<warning descr="Redundant null-check: 'this' object is never null">this</warning>);
|
|
|
|
String s = Objects.requireNonNull(<warning descr="Redundant null-check: literal is never null">" x "</warning>);
|
|
String s1 = trim(" x ");
|
|
|
|
System.out.println(inferred(<warning descr="Redundant null-check: literal is never null">"foo"</warning>));
|
|
}
|
|
|
|
static String concat(String s, String m) {
|
|
if(s == null) throw new NullPointerException();
|
|
return s+m;
|
|
}
|
|
|
|
void testStrings(List<String> list) {
|
|
list.forEach(s -> concat("Not null!", s));
|
|
list.stream().map(s -> concat("Not null!", s)).forEach(System.out::println);
|
|
}
|
|
|
|
@Contract(value="null -> fail", pure=true)
|
|
String trim(String s) {
|
|
return s.trim();
|
|
}
|
|
|
|
@Contract(value="null -> fail", pure=true)
|
|
static void assertNotNull(Object obj) {
|
|
if(obj == null) throw new NullPointerException();
|
|
}
|
|
|
|
@Contract(value="_,!null -> fail", pure=true)
|
|
static void assertNull(String msg, Object obj) {
|
|
if(obj != null) throw new NullPointerException(msg);
|
|
}
|
|
|
|
static String inferred(String str) {
|
|
if(str == null) throw new IllegalArgumentException();
|
|
return str;
|
|
}
|
|
|
|
class X {
|
|
private String foo;
|
|
|
|
X(String f) {
|
|
if(f == null) throw new NullPointerException();
|
|
this.foo = f;
|
|
}
|
|
|
|
X() {
|
|
this("");
|
|
}
|
|
}
|
|
} |