diff --git a/gradle/dependencies.gradle b/gradle/dependencies.gradle index db71894afa..ae1b0177b3 100755 --- a/gradle/dependencies.gradle +++ b/gradle/dependencies.gradle @@ -16,7 +16,8 @@ def versions = [ checkerFramework : "2.5.5", - errorProne : "2.3.2", + errorProne : "2.3.3", // The version of error prone running on this project + errorProneApi : "2.3.1", // The version of error prone built against and shipped support : "27.1.1", wala : "1.5.0-uber.1", commonscli : "1.4", @@ -30,7 +31,7 @@ def apt = [ ] def build = [ - errorProneCheckApi : "com.google.errorprone:error_prone_check_api:${versions.errorProne}", + errorProneCheckApi : "com.google.errorprone:error_prone_check_api:${versions.errorProneApi}", errorProneCore : "com.google.errorprone:error_prone_core:${versions.errorProne}", errorProneJavac : "com.google.errorprone:javac:9+181-r4173-1", errorProneTestHelpers : "com.google.errorprone:error_prone_test_helpers:${versions.errorProne}", diff --git a/jar-infer/jar-infer-lib/src/main/java/com/uber/nullaway/jarinfer/DefinitelyDerefedParams.java b/jar-infer/jar-infer-lib/src/main/java/com/uber/nullaway/jarinfer/DefinitelyDerefedParams.java index 5ce5400df7..8317a17125 100644 --- a/jar-infer/jar-infer-lib/src/main/java/com/uber/nullaway/jarinfer/DefinitelyDerefedParams.java +++ b/jar-infer/jar-infer-lib/src/main/java/com/uber/nullaway/jarinfer/DefinitelyDerefedParams.java @@ -58,9 +58,6 @@ private static void LOG(boolean cond, String tag, String msg) { private final ControlFlowGraph cfg; private PrunedCFG prunedCFG; - // used to resolve references to fields in putstatic instructions - private final IClassHierarchy cha; - /** List of null test APIs and the parameter position. */ private static final ImmutableMap NULL_TEST_APIS = new ImmutableMap.Builder() @@ -95,7 +92,6 @@ public DefinitelyDerefedParams( this.method = method; this.ir = ir; this.cfg = cfg; - this.cha = cha; prunedCFG = null; } diff --git a/jar-infer/jar-infer-lib/src/main/java/com/uber/nullaway/jarinfer/DefinitelyDerefedParamsDriver.java b/jar-infer/jar-infer-lib/src/main/java/com/uber/nullaway/jarinfer/DefinitelyDerefedParamsDriver.java index 71be5794e2..2e3ca0f851 100644 --- a/jar-infer/jar-infer-lib/src/main/java/com/uber/nullaway/jarinfer/DefinitelyDerefedParamsDriver.java +++ b/jar-infer/jar-infer-lib/src/main/java/com/uber/nullaway/jarinfer/DefinitelyDerefedParamsDriver.java @@ -145,7 +145,6 @@ public static Result run(String inPaths, String pkgName, String outPath, boolean DEBUG = dbg; VERBOSE = vbs; long start = System.currentTimeMillis(); - String firstInPath = inPaths.split(",")[0]; Set setInPaths = new HashSet<>(Arrays.asList(inPaths.split(","))); for (String inPath : setInPaths) { InputStream jarIS = null; diff --git a/jar-infer/jar-infer-lib/src/test/java/com/uber/nullaway/jarinfer/JarInferTest.java b/jar-infer/jar-infer-lib/src/test/java/com/uber/nullaway/jarinfer/JarInferTest.java index b7b3dbc378..c76e6c5d5a 100644 --- a/jar-infer/jar-infer-lib/src/test/java/com/uber/nullaway/jarinfer/JarInferTest.java +++ b/jar-infer/jar-infer-lib/src/test/java/com/uber/nullaway/jarinfer/JarInferTest.java @@ -88,12 +88,10 @@ private void testTemplate( /** * Run a unit test with a specified jar file. * - * @param testName An useful name for the unit test. * @param pkg Qualified package name. * @param jarPath Path to the target jar file. */ private void testJARTemplate( - String testName, String pkg, // in dot syntax String jarPath // in dot syntax ) throws Exception { @@ -224,7 +222,6 @@ public void toyNonStatic() throws Exception { @Test public void toyJAR() throws Exception { testJARTemplate( - "toyJAR", "com.uber.nullaway.jarinfer.toys.unannotated", "../test-java-lib-jarinfer/build/libs/test-java-lib-jarinfer.jar"); } diff --git a/nullaway/src/main/java/com/uber/nullaway/NullAway.java b/nullaway/src/main/java/com/uber/nullaway/NullAway.java index 83b91baab1..1a087e3e1a 100644 --- a/nullaway/src/main/java/com/uber/nullaway/NullAway.java +++ b/nullaway/src/main/java/com/uber/nullaway/NullAway.java @@ -22,7 +22,6 @@ package com.uber.nullaway; -import static com.google.errorprone.BugPattern.Category.JDK; import static com.google.errorprone.BugPattern.SeverityLevel.WARNING; import static com.sun.source.tree.Tree.Kind.EXPRESSION_STATEMENT; import static com.sun.source.tree.Tree.Kind.IDENTIFIER; @@ -94,7 +93,6 @@ import com.uber.nullaway.handlers.Handlers; import java.lang.annotation.Annotation; import java.util.ArrayList; -import java.util.Collections; import java.util.LinkedHashMap; import java.util.LinkedHashSet; import java.util.List; @@ -146,7 +144,7 @@ name = "NullAway", altNames = {"CheckNullabilityTypes"}, summary = "Nullability type error.", - category = JDK, + tags = BugPattern.StandardTags.LIKELY_ERROR, severity = WARNING) public class NullAway extends BugChecker implements BugChecker.MethodInvocationTreeMatcher, @@ -430,7 +428,7 @@ public Description matchCompoundAssignment(CompoundAssignmentTree tree, VisitorS } Type lhsType = ASTHelpers.getType(tree.getVariable()); Type stringType = state.getTypeFromString("java.lang.String"); - if (lhsType != null && !lhsType.equals(stringType)) { + if (lhsType != null && !state.getTypes().isSameType(lhsType, stringType)) { // both LHS and RHS could get unboxed return doUnboxingCheck(state, tree.getVariable(), tree.getExpression()); } @@ -904,14 +902,14 @@ private boolean fieldAlwaysInitializedBeforeRead( * href="https://github.com/uber/NullAway/wiki/Error-Messages#initializer-method-does-not-guarantee-nonnull-field-is-initialized--nonnull-field--not-initialized">the * docs for what is considered a safe initializer method. */ - private Set safeInitByCalleeBefore( + private ImmutableSet safeInitByCalleeBefore( TreePath pathToRead, VisitorState state, TreePath enclosingBlockPath) { - Set result = new LinkedHashSet<>(); Set safeInitMethods = new LinkedHashSet<>(); Tree enclosingBlockOrMethod = enclosingBlockPath.getLeaf(); if (enclosingBlockOrMethod instanceof VariableTree) { - return Collections.emptySet(); + return ImmutableSet.of(); } + ImmutableSet.Builder resultBuilder = ImmutableSet.builder(); BlockTree blockTree = enclosingBlockOrMethod instanceof BlockTree ? (BlockTree) enclosingBlockOrMethod @@ -926,7 +924,7 @@ private Set safeInitByCalleeBefore( if (classTreePath == null) { throw new IllegalStateException( "could not find enclosing class / enum / interface for " - + enclosingBlockPath.getLeaf()); + + state.getSourceForNode(enclosingBlockPath.getLeaf())); } } Symbol.ClassSymbol classSymbol = ASTHelpers.getSymbol((ClassTree) classTreePath.getLeaf()); @@ -944,12 +942,12 @@ private Set safeInitByCalleeBefore( // that initialization? if (tryTree.getCatches().size() == 0) { if (tryTree.getBlock() != null) { - result.addAll( + resultBuilder.addAll( safeInitByCalleeBefore( pathToRead, state, new TreePath(enclosingBlockPath, tryTree.getBlock()))); } if (tryTree.getFinallyBlock() != null) { - result.addAll( + resultBuilder.addAll( safeInitByCalleeBefore( pathToRead, state, @@ -960,8 +958,8 @@ private Set safeInitByCalleeBefore( } } addGuaranteedNonNullFromInvokes( - state, getTreesInstance(state), safeInitMethods, getNullnessAnalysis(state), result); - return result; + state, getTreesInstance(state), safeInitMethods, getNullnessAnalysis(state), resultBuilder); + return resultBuilder.build(); } private int getStartPos(JCTree tree) { @@ -1130,7 +1128,7 @@ public Description matchClass(ClassTree tree, VisitorState state) { // but this is not easy within the Error Prone APIs NestingKind nestingKind = classSymbol.getNestingKind(); if (!nestingKind.isNested()) { - matchWithinClass = !isExcludedClass(classSymbol, state); + matchWithinClass = !isExcludedClass(classSymbol); // since we are processing a new top-level class, invalidate any cached // results for previous classes handler.onMatchTopLevelClass(this, tree, state, classSymbol); @@ -1227,7 +1225,8 @@ public Description matchEnhancedForLoop(EnhancedForLoopTree tree, VisitorState s ExpressionTree expr = tree.getExpression(); final ErrorMessage errorMessage = new ErrorMessage( - MessageTypes.DEREFERENCE_NULLABLE, "enhanced-for expression " + expr + " is @Nullable"); + MessageTypes.DEREFERENCE_NULLABLE, + "enhanced-for expression " + state.getSourceForNode(expr) + " is @Nullable"); if (mayBeNullExpr(state, expr)) { return errorBuilder.createErrorDescription( errorMessage, state.getPath(), buildDescription(expr)); @@ -1329,7 +1328,9 @@ private Description handleInvocation( // make sure we are passing a non-null value if (mayActualBeNull) { String message = - "passing @Nullable parameter '" + actual.toString() + "' where @NonNull is required"; + "passing @Nullable parameter '" + + state.getSourceForNode(actual) + + "' where @NonNull is required"; return errorBuilder.createErrorDescriptionForNullAssignment( new ErrorMessage(MessageTypes.PASS_NULLABLE, message), actual, @@ -1371,7 +1372,7 @@ private Description checkCastToNonNullTakesNullable( if (!isInitializer && !mayBeNullExpr(state, actual)) { String message = "passing known @NonNull parameter '" - + actual.toString() + + state.getSourceForNode(actual) + "' to CastToNonNullMethod (" + qualifiedName + "). This method should only take arguments that NullAway considers @Nullable " @@ -1465,7 +1466,7 @@ private Set notAssignedInAnyInitializer( FieldInitEntities entities, Set notInitializedInConstructors, VisitorState state) { Trees trees = getTreesInstance(state); Symbol.ClassSymbol classSymbol = entities.classSymbol(); - Set initInSomeInitializer = new LinkedHashSet<>(); + ImmutableSet.Builder initInSomeInitializerBuilder = ImmutableSet.builder(); for (MethodTree initMethodTree : entities.instanceInitializerMethods()) { if (initMethodTree.getBody() == null) { continue; @@ -1474,7 +1475,7 @@ private Set notAssignedInAnyInitializer( state, trees, classSymbol, - initInSomeInitializer, + initInSomeInitializerBuilder, initMethodTree.getBody(), new TreePath(state.getPath(), initMethodTree)); } @@ -1483,11 +1484,12 @@ private Set notAssignedInAnyInitializer( state, trees, classSymbol, - initInSomeInitializer, + initInSomeInitializerBuilder, block, new TreePath(state.getPath(), block)); } Set result = new LinkedHashSet<>(); + ImmutableSet initInSomeInitializer = initInSomeInitializerBuilder.build(); for (Symbol fieldSymbol : notInitializedInConstructors) { if (!initInSomeInitializer.contains(fieldSymbol)) { result.add(fieldSymbol); @@ -1500,16 +1502,16 @@ private void addInitializedFieldsForBlock( VisitorState state, Trees trees, Symbol.ClassSymbol classSymbol, - Set initInSomeInitializer, + ImmutableSet.Builder initInSomeInitializerBuilder, BlockTree block, TreePath path) { AccessPathNullnessAnalysis nullnessAnalysis = getNullnessAnalysis(state); Set nonnullAtExit = nullnessAnalysis.getNonnullFieldsOfReceiverAtExit(path, state.context); - initInSomeInitializer.addAll(nonnullAtExit); + initInSomeInitializerBuilder.addAll(nonnullAtExit); Set safeInitMethods = getSafeInitMethods(block, classSymbol, state); addGuaranteedNonNullFromInvokes( - state, trees, safeInitMethods, nullnessAnalysis, initInSomeInitializer); + state, trees, safeInitMethods, nullnessAnalysis, initInSomeInitializerBuilder); } /** @@ -1548,19 +1550,18 @@ private boolean isExternalInit(Symbol.ClassSymbol classSymbol) { .anyMatch(config::isExternalInitClassAnnotation); } - private Set guaranteedNonNullForConstructor( + private ImmutableSet guaranteedNonNullForConstructor( FieldInitEntities entities, VisitorState state, Trees trees, MethodTree constructor) { - Symbol.MethodSymbol symbol = ASTHelpers.getSymbol(constructor); Set safeInitMethods = getSafeInitMethods(constructor.getBody(), entities.classSymbol(), state); AccessPathNullnessAnalysis nullnessAnalysis = getNullnessAnalysis(state); - Set guaranteedNonNull = new LinkedHashSet<>(); - guaranteedNonNull.addAll( + ImmutableSet.Builder guaranteedNonNullBuilder = ImmutableSet.builder(); + guaranteedNonNullBuilder.addAll( nullnessAnalysis.getNonnullFieldsOfReceiverAtExit( new TreePath(state.getPath(), constructor), state.context)); addGuaranteedNonNullFromInvokes( - state, trees, safeInitMethods, nullnessAnalysis, guaranteedNonNull); - return guaranteedNonNull; + state, trees, safeInitMethods, nullnessAnalysis, guaranteedNonNullBuilder); + return guaranteedNonNullBuilder.build(); } /** does the constructor invoke another constructor in the same class via this(...)? */ @@ -1606,10 +1607,10 @@ private void addGuaranteedNonNullFromInvokes( Trees trees, Set safeInitMethods, AccessPathNullnessAnalysis nullnessAnalysis, - Set guaranteedNonNull) { + ImmutableSet.Builder guaranteedNonNullBuilder) { for (Element invoked : safeInitMethods) { Tree invokedTree = trees.getTree(invoked); - guaranteedNonNull.addAll( + guaranteedNonNullBuilder.addAll( nullnessAnalysis.getNonnullFieldsOfReceiverAtExit( new TreePath(state.getPath(), invokedTree), state.context)); } @@ -1759,7 +1760,8 @@ private FieldInitEntities collectEntities(ClassTree tree, VisitorState state) { // do nothing break; default: - throw new RuntimeException(memberTree.getKind().toString() + " " + memberTree); + throw new RuntimeException( + memberTree.getKind().toString() + " " + state.getSourceForNode(memberTree)); } } @@ -1804,7 +1806,7 @@ private boolean skipDueToFieldAnnotation(Symbol fieldSymbol) { .anyMatch(config::isExcludedFieldAnnotation); } - private boolean isExcludedClass(Symbol.ClassSymbol classSymbol, VisitorState state) { + private boolean isExcludedClass(Symbol.ClassSymbol classSymbol) { String className = classSymbol.getQualifiedName().toString(); if (config.isExcludedClass(className)) { return true; @@ -1910,7 +1912,8 @@ private boolean mayBeNullExpr(VisitorState state, ExpressionTree expr) { exprMayBeNull = nullnessFromDataflow(state, expr); break; default: - throw new RuntimeException("whoops, better handle " + expr.getKind() + " " + expr); + throw new RuntimeException( + "whoops, better handle " + expr.getKind() + " " + state.getSourceForNode(expr)); } exprMayBeNull = handler.onOverrideMayBeNullExpr(this, expr, state, exprMayBeNull); return exprMayBeNull; @@ -1983,7 +1986,7 @@ private Description matchDereference( } if (mayBeNullExpr(state, baseExpression)) { final String message = - "dereferenced expression " + baseExpression.toString() + " is @Nullable"; + "dereferenced expression " + state.getSourceForNode(baseExpression) + " is @Nullable"; ErrorMessage errorMessage = new ErrorMessage(MessageTypes.DEREFERENCE_NULLABLE, message); handler.onPrepareErrorMessage(baseExpression, state, errorMessage); @@ -1996,7 +1999,7 @@ private Description matchDereference( @SuppressWarnings("unused") private Description.Builder changeReturnNullabilityFix( - Tree suggestTree, Description.Builder builder) { + Tree suggestTree, Description.Builder builder, VisitorState state) { if (suggestTree.getKind() != Tree.Kind.METHOD) { throw new RuntimeException("This should be a MethodTree"); } @@ -2004,7 +2007,7 @@ private Description.Builder changeReturnNullabilityFix( MethodTree methodTree = (MethodTree) suggestTree; int countNullableAnnotations = 0; for (AnnotationTree annotationTree : methodTree.getModifiers().getAnnotations()) { - if (annotationTree.getAnnotationType().toString().endsWith("Nullable")) { + if (state.getSourceForNode(annotationTree.getAnnotationType()).endsWith("Nullable")) { fixBuilder.delete(annotationTree); countNullableAnnotations += 1; } @@ -2094,7 +2097,7 @@ private Symbol.MethodSymbol getClosestOverriddenMethod(Symbol.MethodSymbol metho // taken from Error Prone MethodOverrides check Symbol.ClassSymbol owner = method.enclClass(); for (Type s : types.closure(owner.type)) { - if (s.equals(owner.type)) { + if (types.isSameType(s, owner.type)) { continue; } for (Symbol m : s.tsym.members().getSymbolsByName(method.name)) { @@ -2162,51 +2165,51 @@ static FieldInitEntities create( Set staticInitializerMethods) { return new AutoValue_NullAway_FieldInitEntities( classSymbol, - nonnullInstanceFields, - nonnullStaticFields, - instanceInitializerBlocks, - staticInitializerBlocks, - constructors, - instanceInitializerMethods, - staticInitializerMethods); + ImmutableSet.copyOf(nonnullInstanceFields), + ImmutableSet.copyOf(nonnullStaticFields), + ImmutableList.copyOf(instanceInitializerBlocks), + ImmutableList.copyOf(staticInitializerBlocks), + ImmutableSet.copyOf(constructors), + ImmutableSet.copyOf(instanceInitializerMethods), + ImmutableSet.copyOf(staticInitializerMethods)); } /** @return symbol for class */ abstract Symbol.ClassSymbol classSymbol(); /** @return @NonNull instance fields that are not directly initialized at declaration */ - abstract Set nonnullInstanceFields(); + abstract ImmutableSet nonnullInstanceFields(); /** @return @NonNull static fields that are not directly initialized at declaration */ - abstract Set nonnullStaticFields(); + abstract ImmutableSet nonnullStaticFields(); /** * @return the list of instance initializer blocks (e.g. blocks of the form `class X { { //Code * } } ), in the order in which they appear in the class */ - abstract List instanceInitializerBlocks(); + abstract ImmutableList instanceInitializerBlocks(); /** * @return the list of static initializer blocks (e.g. blocks of the form `class X { static { * //Code } } ), in the order in which they appear in the class */ - abstract List staticInitializerBlocks(); + abstract ImmutableList staticInitializerBlocks(); /** @return the list of constructor */ - abstract Set constructors(); + abstract ImmutableSet constructors(); /** * @return the list of non-static (instance) initializer methods. This includes methods * annotated @Initializer, as well as those specified by -XepOpt:NullAway:KnownInitializers * or annotated with annotations passed to -XepOpt:NullAway:CustomInitializerAnnotations */ - abstract Set instanceInitializerMethods(); + abstract ImmutableSet instanceInitializerMethods(); /** * @return the list of static initializer methods. This includes static methods * annotated @Initializer, as well as those specified by -XepOpt:NullAway:KnownInitializers * or annotated with annotations passed to -XepOpt:NullAway:CustomInitializerAnnotations */ - abstract Set staticInitializerMethods(); + abstract ImmutableSet staticInitializerMethods(); } } diff --git a/nullaway/src/main/java/com/uber/nullaway/handlers/LibraryModelsHandler.java b/nullaway/src/main/java/com/uber/nullaway/handlers/LibraryModelsHandler.java index b8a1b58a50..7f4af2fd1e 100644 --- a/nullaway/src/main/java/com/uber/nullaway/handlers/LibraryModelsHandler.java +++ b/nullaway/src/main/java/com/uber/nullaway/handlers/LibraryModelsHandler.java @@ -124,7 +124,7 @@ public NullnessHint onDataflowVisitMethodInvocation( Symbol.MethodSymbol callee = ASTHelpers.getSymbol(node.getTree()); Preconditions.checkNotNull(callee); setUnconditionalArgumentNullness(bothUpdates, node.getArguments(), callee, context); - setConditionalArgumentNullness(thenUpdates, elseUpdates, node.getArguments(), callee, context); + setConditionalArgumentNullness(elseUpdates, node.getArguments(), callee, context); if (getOptLibraryModels(context).hasNonNullReturn(callee, types)) { return NullnessHint.FORCE_NONNULL; } else if (getOptLibraryModels(context).hasNullableReturn(callee, types)) { @@ -135,7 +135,6 @@ public NullnessHint onDataflowVisitMethodInvocation( } private void setConditionalArgumentNullness( - AccessPathNullnessPropagation.Updates thenUpdates, AccessPathNullnessPropagation.Updates elseUpdates, List arguments, Symbol.MethodSymbol callee, diff --git a/nullaway/src/main/java/com/uber/nullaway/handlers/RxNullabilityPropagator.java b/nullaway/src/main/java/com/uber/nullaway/handlers/RxNullabilityPropagator.java index 7fb6098459..1419049c00 100644 --- a/nullaway/src/main/java/com/uber/nullaway/handlers/RxNullabilityPropagator.java +++ b/nullaway/src/main/java/com/uber/nullaway/handlers/RxNullabilityPropagator.java @@ -234,7 +234,6 @@ public void onMatchMethodInvocation( Type receiverType = ASTHelpers.getReceiverType(tree); for (StreamTypeRecord streamType : RX_MODELS) { if (streamType.matchesType(receiverType, state)) { - String methodName = methodSymbol.toString(); // Build observable call chain buildObservableCallChain(tree); @@ -262,7 +261,7 @@ public void onMatchMethodInvocation( // Ensure that this `new B() ...` has a custom class body, otherwise, we skip for now. if (annonClassBody != null) { MaplikeMethodRecord methodRecord = streamType.getMaplikeMethodRecord(methodSymbol); - handleMapAnonClass(methodRecord, tree, annonClassBody, state); + handleMapAnonClass(methodRecord, tree, annonClassBody); } } else if (argTree instanceof LambdaExpressionTree) { observableCallToInnerMethodOrLambda.put(tree, (LambdaExpressionTree) argTree); @@ -298,7 +297,6 @@ private void handleChainFromFilter( outerCallInChain = observableOuterCallInChain.get(outerCallInChain); // Check for a map method (which might be a pass-through method or the first method after a // pass-through chain) - MethodInvocationTree mapCallsite = observableOuterCallInChain.get(observableDotFilter); if (observableCallToInnerMethodOrLambda.containsKey(outerCallInChain)) { // Update mapToFilterMap Symbol.MethodSymbol mapMethod = ASTHelpers.getSymbol(outerCallInChain); @@ -341,8 +339,7 @@ private void handleFilterLambda( private void handleMapAnonClass( MaplikeMethodRecord methodRecord, MethodInvocationTree observableDotMap, - ClassTree annonClassBody, - VisitorState state) { + ClassTree annonClassBody) { for (Tree t : annonClassBody.getMembers()) { if (t instanceof MethodTree && ((MethodTree) t).getName().toString().equals(methodRecord.getInnerMethodName())) { diff --git a/nullaway/src/test/java/com/uber/nullaway/NullAwayAndroidTest.java b/nullaway/src/test/java/com/uber/nullaway/NullAwayAndroidTest.java index 5282614231..ef2f622955 100644 --- a/nullaway/src/test/java/com/uber/nullaway/NullAwayAndroidTest.java +++ b/nullaway/src/test/java/com/uber/nullaway/NullAwayAndroidTest.java @@ -17,6 +17,7 @@ public class NullAwayAndroidTest { private CompilationTestHelper compilationHelper; + @SuppressWarnings("CheckReturnValue") @Before public void setup() { compilationHelper = CompilationTestHelper.newInstance(NullAway.class, getClass()); @@ -174,6 +175,7 @@ public void androidxActivitySuccess() { } /** Initialises the default android classes that are commonly used. */ + @SuppressWarnings("CheckReturnValue") private void initialiseAndroidCoreClasses() { compilationHelper .addSourceFile("androidstubs/core/Context.java") diff --git a/nullaway/src/test/java/com/uber/nullaway/NullAwayAutoSuggestNoCastTest.java b/nullaway/src/test/java/com/uber/nullaway/NullAwayAutoSuggestNoCastTest.java index 33533e260c..e911926dbd 100644 --- a/nullaway/src/test/java/com/uber/nullaway/NullAwayAutoSuggestNoCastTest.java +++ b/nullaway/src/test/java/com/uber/nullaway/NullAwayAutoSuggestNoCastTest.java @@ -60,8 +60,8 @@ public void suggestSuppressionWithComment() { BugCheckerRefactoringTestHelper bcr = BugCheckerRefactoringTestHelper.newInstance(new NullAway(flags), getClass()); - bcr.setArgs("-d", temporaryFolder.getRoot().getAbsolutePath()); - bcr.addInputLines( + bcr.setArgs("-d", temporaryFolder.getRoot().getAbsolutePath()) + .addInputLines( "Test.java", "package com.uber;", "class Test {", @@ -76,8 +76,8 @@ public void suggestSuppressionWithComment() { " @SuppressWarnings(\"NullAway\") /* PR #000000 */ Object test1() {", " return null;", " }", - "}"); - bcr.doTest(BugCheckerRefactoringTestHelper.TestMode.TEXT_MATCH); // Yes we can! + "}") + .doTest(BugCheckerRefactoringTestHelper.TestMode.TEXT_MATCH); // Yes we can! } @Test @@ -86,8 +86,8 @@ public void suggestSuppressionWithoutComment() { BugCheckerRefactoringTestHelper.newInstance( new NullAway(flagsNoAutoFixSuppressionComment), getClass()); - bcr.setArgs("-d", temporaryFolder.getRoot().getAbsolutePath()); - bcr.addInputLines( + bcr.setArgs("-d", temporaryFolder.getRoot().getAbsolutePath()) + .addInputLines( "Test.java", "package com.uber;", "class Test {", @@ -102,8 +102,8 @@ public void suggestSuppressionWithoutComment() { " @SuppressWarnings(\"NullAway\") Object test1() {", " return null;", " }", - "}"); - bcr.doTest(BugCheckerRefactoringTestHelper.TestMode.TEXT_MATCH); + "}") + .doTest(BugCheckerRefactoringTestHelper.TestMode.TEXT_MATCH); } @Test @@ -112,8 +112,8 @@ public void suggestSuppressionFieldLambdaDeref() { BugCheckerRefactoringTestHelper.newInstance( new NullAway(flagsNoAutoFixSuppressionComment), getClass()); - bcr.setArgs("-d", temporaryFolder.getRoot().getAbsolutePath()); - bcr.addInputLines( + bcr.setArgs("-d", temporaryFolder.getRoot().getAbsolutePath()) + .addInputLines( "Test.java", "package com.uber;", "import javax.annotation.Nullable;", @@ -135,8 +135,8 @@ public void suggestSuppressionFieldLambdaDeref() { " () -> {", " foo.toString();", " };", - "}"); - bcr.doTest(BugCheckerRefactoringTestHelper.TestMode.TEXT_MATCH); + "}") + .doTest(BugCheckerRefactoringTestHelper.TestMode.TEXT_MATCH); } @Test @@ -145,8 +145,8 @@ public void suggestSuppressionFieldLambdaUnbox() { BugCheckerRefactoringTestHelper.newInstance( new NullAway(flagsNoAutoFixSuppressionComment), getClass()); - bcr.setArgs("-d", temporaryFolder.getRoot().getAbsolutePath()); - bcr.addInputLines( + bcr.setArgs("-d", temporaryFolder.getRoot().getAbsolutePath()) + .addInputLines( "Test.java", "package com.uber;", "import javax.annotation.Nullable;", @@ -170,8 +170,8 @@ public void suggestSuppressionFieldLambdaUnbox() { " () -> {", " id(foo);", " };", - "}"); - bcr.doTest(BugCheckerRefactoringTestHelper.TestMode.TEXT_MATCH); + "}") + .doTest(BugCheckerRefactoringTestHelper.TestMode.TEXT_MATCH); } @Test @@ -180,8 +180,8 @@ public void suggestSuppressionFieldLambdaAssignment() { BugCheckerRefactoringTestHelper.newInstance( new NullAway(flagsNoAutoFixSuppressionComment), getClass()); - bcr.setArgs("-d", temporaryFolder.getRoot().getAbsolutePath()); - bcr.addInputLines( + bcr.setArgs("-d", temporaryFolder.getRoot().getAbsolutePath()) + .addInputLines( "Test.java", "package com.uber;", "import javax.annotation.Nullable;", @@ -205,8 +205,8 @@ public void suggestSuppressionFieldLambdaAssignment() { " @SuppressWarnings(\"NullAway\")", " int x = foo + 1;", " };", - "}"); - bcr.doTest(BugCheckerRefactoringTestHelper.TestMode.TEXT_MATCH); + "}") + .doTest(BugCheckerRefactoringTestHelper.TestMode.TEXT_MATCH); } @Test @@ -215,8 +215,8 @@ public void suggestLambdaAssignInMethod() { BugCheckerRefactoringTestHelper.newInstance( new NullAway(flagsNoAutoFixSuppressionComment), getClass()); - bcr.setArgs("-d", temporaryFolder.getRoot().getAbsolutePath()); - bcr.addInputLines( + bcr.setArgs("-d", temporaryFolder.getRoot().getAbsolutePath()) + .addInputLines( "Test.java", "package com.uber;", "import javax.annotation.Nullable;", @@ -245,8 +245,8 @@ public void suggestLambdaAssignInMethod() { " @SuppressWarnings(\"NullAway\")", " java.util.function.Function g = (x) -> { return foo + 1; };", " }", - "}"); - bcr.doTest(BugCheckerRefactoringTestHelper.TestMode.TEXT_MATCH); + "}") + .doTest(BugCheckerRefactoringTestHelper.TestMode.TEXT_MATCH); } @Test @@ -255,8 +255,8 @@ public void suppressMethodRefOverrideParam() { BugCheckerRefactoringTestHelper.newInstance( new NullAway(flagsNoAutoFixSuppressionComment), getClass()); - bcr.setArgs("-d", temporaryFolder.getRoot().getAbsolutePath()); - bcr.addInputLines( + bcr.setArgs("-d", temporaryFolder.getRoot().getAbsolutePath()) + .addInputLines( "Test.java", "package com.uber;", "import javax.annotation.Nullable;", @@ -284,8 +284,8 @@ public void suppressMethodRefOverrideParam() { " static void bar() {", " callFoo(Test::biz);", " }", - "}"); - bcr.doTest(BugCheckerRefactoringTestHelper.TestMode.TEXT_MATCH); + "}") + .doTest(BugCheckerRefactoringTestHelper.TestMode.TEXT_MATCH); } @Test @@ -294,8 +294,8 @@ public void suppressMethodRefOverrideReturn() { BugCheckerRefactoringTestHelper.newInstance( new NullAway(flagsNoAutoFixSuppressionComment), getClass()); - bcr.setArgs("-d", temporaryFolder.getRoot().getAbsolutePath()); - bcr.addInputLines( + bcr.setArgs("-d", temporaryFolder.getRoot().getAbsolutePath()) + .addInputLines( "Test.java", "package com.uber;", "import javax.annotation.Nullable;", @@ -325,7 +325,7 @@ public void suppressMethodRefOverrideReturn() { " static void bar() {", " callFoo(Test::biz);", " }", - "}"); - bcr.doTest(BugCheckerRefactoringTestHelper.TestMode.TEXT_MATCH); + "}") + .doTest(BugCheckerRefactoringTestHelper.TestMode.TEXT_MATCH); } } diff --git a/nullaway/src/test/java/com/uber/nullaway/NullAwayAutoSuggestTest.java b/nullaway/src/test/java/com/uber/nullaway/NullAwayAutoSuggestTest.java index 78f166216e..5c99701460 100644 --- a/nullaway/src/test/java/com/uber/nullaway/NullAwayAutoSuggestTest.java +++ b/nullaway/src/test/java/com/uber/nullaway/NullAwayAutoSuggestTest.java @@ -53,8 +53,8 @@ public void correctCastToNonNull() throws IOException { BugCheckerRefactoringTestHelper bcr = BugCheckerRefactoringTestHelper.newInstance(new NullAway(flags), getClass()); - bcr.setArgs("-d", temporaryFolder.getRoot().getAbsolutePath()); - bcr.addInputLines( + bcr.setArgs("-d", temporaryFolder.getRoot().getAbsolutePath()) + .addInputLines( "Test.java", "package com.uber;", "import javax.annotation.Nullable;", @@ -64,8 +64,8 @@ public void correctCastToNonNull() throws IOException { " return castToNonNull(o);", " }", "}") - .expectUnchanged(); - bcr.doTest(); + .expectUnchanged() + .doTest(); } @Test @@ -73,8 +73,8 @@ public void suggestCastToNonNull() throws IOException { BugCheckerRefactoringTestHelper bcr = BugCheckerRefactoringTestHelper.newInstance(new NullAway(flags), getClass()); - bcr.setArgs("-d", temporaryFolder.getRoot().getAbsolutePath()); - bcr.addInputLines( + bcr.setArgs("-d", temporaryFolder.getRoot().getAbsolutePath()) + .addInputLines( "Test.java", "package com.uber;", "import javax.annotation.Nullable;", @@ -92,8 +92,8 @@ public void suggestCastToNonNull() throws IOException { " Object test1(@Nullable Object o) {", " return castToNonNull(o);", " }", - "}"); - bcr.doTest(); + "}") + .doTest(); } @Test @@ -101,8 +101,8 @@ public void removeUnnecessaryCastToNonNull() throws IOException { BugCheckerRefactoringTestHelper bcr = BugCheckerRefactoringTestHelper.newInstance(new NullAway(flags), getClass()); - bcr.setArgs("-d", temporaryFolder.getRoot().getAbsolutePath()); - bcr.addInputLines( + bcr.setArgs("-d", temporaryFolder.getRoot().getAbsolutePath()) + .addInputLines( "Test.java", "package com.uber;", "import javax.annotation.Nullable;", @@ -121,8 +121,8 @@ public void removeUnnecessaryCastToNonNull() throws IOException { " Object test1(Object o) {", " return o;", " }", - "}"); - bcr.doTest(); + "}") + .doTest(); } @Test @@ -130,8 +130,8 @@ public void suggestSuppressionOnMethodRef() throws IOException { BugCheckerRefactoringTestHelper bcr = BugCheckerRefactoringTestHelper.newInstance(new NullAway(flags), getClass()); - bcr.setArgs("-d", temporaryFolder.getRoot().getAbsolutePath()); - bcr.addInputLines( + bcr.setArgs("-d", temporaryFolder.getRoot().getAbsolutePath()) + .addInputLines( "Test.java", "package com.uber;", "import javax.annotation.Nullable;", @@ -166,7 +166,7 @@ public void suggestSuppressionOnMethodRef() throws IOException { " @SuppressWarnings(\"NullAway\") void test() throws Exception {", " takesNonNull(execute(Test::doReturnNullable));", " }", - "}"); - bcr.doTest(); + "}") + .doTest(); } } diff --git a/nullaway/src/test/java/com/uber/nullaway/NullAwayTest.java b/nullaway/src/test/java/com/uber/nullaway/NullAwayTest.java index dff3d50a53..e961a06ee0 100644 --- a/nullaway/src/test/java/com/uber/nullaway/NullAwayTest.java +++ b/nullaway/src/test/java/com/uber/nullaway/NullAwayTest.java @@ -40,6 +40,7 @@ public class NullAwayTest { private CompilationTestHelper compilationHelper; + @SuppressWarnings("CheckReturnValue") @Before public void setup() { compilationHelper = CompilationTestHelper.newInstance(NullAway.class, getClass()); diff --git a/sample-app/src/main/java/com/uber/myapplication/MainActivity.java b/sample-app/src/main/java/com/uber/myapplication/MainActivity.java index 14b99011d5..88219a95f5 100644 --- a/sample-app/src/main/java/com/uber/myapplication/MainActivity.java +++ b/sample-app/src/main/java/com/uber/myapplication/MainActivity.java @@ -22,6 +22,7 @@ import org.utilities.StringUtils; /** Sample activity. */ +@SuppressWarnings("UnusedVariable") // This is sample code public class MainActivity extends AppCompatActivity { @NonNull private Object mOnCreateInitialiedField; diff --git a/sample-app/src/main/java/com/uber/myapplication/MainFragment.java b/sample-app/src/main/java/com/uber/myapplication/MainFragment.java index dcc45f9a5c..5212a04a75 100644 --- a/sample-app/src/main/java/com/uber/myapplication/MainFragment.java +++ b/sample-app/src/main/java/com/uber/myapplication/MainFragment.java @@ -9,6 +9,7 @@ import android.view.View; import android.view.ViewGroup; +@SuppressWarnings("UnusedVariable") // This is sample code public class MainFragment extends Fragment { @NonNull private Object mOnCreateInitialisedField; diff --git a/sample/src/main/java/com/uber/mylib/Lambdas.java b/sample/src/main/java/com/uber/mylib/Lambdas.java index 585d94deba..10c5d5a952 100644 --- a/sample/src/main/java/com/uber/mylib/Lambdas.java +++ b/sample/src/main/java/com/uber/mylib/Lambdas.java @@ -8,6 +8,7 @@ import javax.annotation.Nullable; /** Code that uses Java 8 lambdas */ +@SuppressWarnings("UnusedVariable") // This is sample code public class Lambdas { @FunctionalInterface