Files
openide/java/java-tests/testData/inspection/dataFlow/fixture/InstanceOfPattern.java
Tagir Valeev a0a8ed752b [java-intentions] Suggest 'replace with null-check' fix if pattern variable is present but unused
Fixes IDEA-265543 Pattern type is the same as expression type error missing fix

GitOrigin-RevId: a971caf5480cd29e50cfc271ae76891fbc750d6b
2021-04-01 05:52:42 +00:00

32 lines
822 B
Java

import java.util.function.*;
import org.jetbrains.annotations.Nullable;
public class InstanceOfPattern {
void test(Foo foo) {
if (foo.bar() instanceof String s) {
System.out.println(s.length());
}
}
void test(Object obj) {
if (obj instanceof Number n) {
if (<warning descr="Condition 'n == obj' is always 'true'">n == obj</warning>) {}
}
}
void testNullCheck(String s) {
if (s instanceof <error descr="Pattern type 'String' is the same as expression type">String</error> s1) {
System.out.println(s1);
}
}
void testNullCheckUnusedPatternVariable(String s) {
if (s instanceof <error descr="Pattern type 'String' is the same as expression type">String</error> s1) {
System.out.println("foo");
}
}
interface Foo {
@Nullable Object bar();
}
}