IDEA-163909 Inspection to replace computeIfAbsent returning a constant with putIfAbsent

This commit is contained in:
Tagir Valeev
2016-11-30 15:15:58 +07:00
parent 43e5739f04
commit d749f3dc5a
15 changed files with 303 additions and 1 deletions
@@ -0,0 +1,10 @@
// "Use 'putIfAbsent' method without lambda" "true"
import java.util.Map;
class Test {
public void test(Map<String, String> map, String key) {
/*comment in param*/
/*comment in arrow*/
map.putIfAbsent(key, (/*comment in parens*/"empty"));
}
}
@@ -0,0 +1,17 @@
// "Use 'or' method without lambda" "true"
package com.google.common.base;
interface Supplier<T> {
T supply();
}
abstract class Optional<T> {
abstract T or(T value);
abstract T or(Supplier<? extends T> supplier);
}
class Test {
public void test(Optional<String> opt) {
System.out.println(opt.or(""));
}
}
@@ -0,0 +1,8 @@
// "Use 'orElse' method without lambda" "true"
import java.util.*;
class Test {
public String test(List<String> data) {
return data.stream().filter(Objects::nonNull).findFirst().orElse(null);
}
}
@@ -0,0 +1,8 @@
// "Use 'putIfAbsent' method without lambda" "true"
import java.util.Map;
class Test {
public void test(Map<String, String> map, String key) {
map.computeIfAbsent(key, (/*comment in param*/k) /*comment in arrow*/<caret>-> (/*comment in parens*/"empty"));
}
}
@@ -0,0 +1,8 @@
// "Use 'putIfAbsent' method without lambda" "false"
import java.util.Map;
class Test {
public void test(Map<String, String> map, String key) {
map.computeIfAbsent(key, (/*comment in param*/k) /*comment in arrow*/<caret>-> (/*comment in parens*/"empty"+key));
}
}
@@ -0,0 +1,8 @@
// "Use 'putIfAbsent' method without lambda" "false"
import java.util.Map;
class Test {
public String test(Map<String, String> map, String key) {
return map.computeIfAbsent(key, k <caret>-> ("empty"));
}
}
@@ -0,0 +1,17 @@
// "Use 'or' method without lambda" "true"
package com.google.common.base;
interface Supplier<T> {
T supply();
}
abstract class Optional<T> {
abstract T or(T value);
abstract T or(Supplier<? extends T> supplier);
}
class Test {
public void test(Optional<String> opt) {
System.out.println(opt.or(() <caret>-> ""));
}
}
@@ -0,0 +1,17 @@
// "Use 'or' method without lambda" "false"
package com.google.common.base;
interface Supplier<T> {
T supply();
}
abstract class Optional<T> {
abstract T or(T value);
abstract T or(Supplier<? extends T> supplier);
}
class Test {
public void test(Optional<Supplier<String>> opt) {
System.out.println(opt.or(() <caret>-> ""));
}
}
@@ -0,0 +1,8 @@
// "Use 'orElse' method without lambda" "true"
import java.util.*;
class Test {
public String test(List<String> data) {
return data.stream().filter(Objects::nonNull).findFirst().orElseGet((<caret>) -> null);
}
}