Repository navigation
Avoid crashes on malformed @Contract arity (#1726) #1756
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -108,6 +108,59 @@ Object violates() { | |
| .doTest(); | ||
| } | ||
|
|
||
| @Test | ||
| public void checkContractMismatchedArity() { | ||
| makeTestHelperWithArgs( | ||
| Arrays.asList( | ||
| "-d", | ||
| temporaryFolder.getRoot().getAbsolutePath(), | ||
| "-XepOpt:NullAway:AnnotatedPackages=com.uber", | ||
| "-XepOpt:NullAway:CheckContracts=true")) | ||
| .addSourceLines( | ||
| "Test.java", | ||
| """ | ||
| package com.uber; | ||
| import org.jetbrains.annotations.Contract; | ||
| public class Test { | ||
| @Contract("!null -> !null") | ||
| // BUG: Diagnostic contains: Invalid @Contract annotation | ||
| public Object[] toArray() { | ||
| return new Object[0]; | ||
| } | ||
| } | ||
| """) | ||
| .doTest(); | ||
| } | ||
|
|
||
| @Test | ||
| public void contractOnVarargsMethodIsNotReportedAsMalformed() { | ||
| makeTestHelperWithArgs( | ||
| Arrays.asList( | ||
| "-d", | ||
| temporaryFolder.getRoot().getAbsolutePath(), | ||
| "-XepOpt:NullAway:AnnotatedPackages=com.uber", | ||
| "-XepOpt:NullAway:CheckContracts=true")) | ||
| .addSourceLines( | ||
| "Test.java", | ||
| """ | ||
| package com.uber; | ||
| import org.jetbrains.annotations.Contract; | ||
| public class Test { | ||
| @Contract("_, !null -> !null") | ||
| static Object varargs(Object first, Object... rest) { | ||
| return new Object(); | ||
| } | ||
| static void calls() { | ||
| varargs(new Object()); | ||
| varargs(new Object(), new Object()); | ||
| varargs(new Object(), new Object(), new Object()); | ||
| varargs(new Object(), new Object[0]); | ||
| } | ||
| } | ||
| """) | ||
| .doTest(); | ||
| } | ||
|
Comment on lines
+135
to
+162
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🎯 Functional Correctness | 🟠 Major | ⚡ Quick win 🔎 Supported by static analysis🏁 Script executed: printf '%s\n' '--- repository conventions ---'
head -5 /tmp/coderabbit-repo-knowledge/uber-nullaway-4b0a71dd/*/*.md 2>/dev/null || true
printf '%s\n' '--- test hunk ---'
sed -n '110,180p' nullaway/src/test/java/com/uber/nullaway/ContractsTests.java
printf '%s\n' '--- bound symbols ---'
rg -n -A35 -B10 'getAntecedent|class ContractHandler|ContractHandler|varargs|MethodInvocationNode' nullaway/src/main nullaway/src/test/java/com/uber/nullaway/ContractsTests.javaRepository: uber/NullAway Length of output: 50370 🏁 Script executed: set -eu
printf '%s\n' '--- relevant repository guidance files ---'
find /tmp/coderabbit-repo-knowledge/uber-nullaway-4b0a71dd -type f -name '*.md' -maxdepth 3 -print
printf '%s\n' '--- contract symbols ---'
rg -n --glob '*.java' 'class ContractHandler|class ContractUtils|getAntecedent|onDataflowVisitMethodInvocation' nullaway/src/main
printf '%s\n' '--- test excerpt ---'
sed -n '125,170p' nullaway/src/test/java/com/uber/nullaway/ContractsTests.java
printf '%s\n' '--- handler and utility excerpts ---'
rg -l --glob '*.java' 'class ContractHandler|class ContractUtils' nullaway/src/main | while read -r f; do
echo "FILE $f"
rg -n -A45 -B10 'class ContractHandler|class ContractUtils|getAntecedent|onDataflowVisitMethodInvocation' "$f"
done
printf '%s\n' '--- MethodInvocationNode definitions/usages ---'
rg -n --glob '*.java' 'class MethodInvocationNode|\.getArguments\(\)|getArguments\(\)' nullaway/src/mainRepository: uber/NullAway Length of output: 30855 🏁 Script executed: set -eu
printf '%s\n' '--- local MethodInvocationNode sources ---'
find . /tmp -type f \( -name 'MethodInvocationNode.java' -o -name '*checker*framework*.jar' \) -print 2>/dev/null | head -50
printf '%s\n' '--- remaining ContractHandler dataflow branch ---'
sed -n '311,430p' nullaway/src/main/java/com/uber/nullaway/handlers/contract/ContractHandler.java
printf '%s\n' '--- invocation argument construction ---'
sed -n '1,90p' nullaway/src/main/java/com/uber/nullaway/InvocationArguments.java
sed -n '1080,1130p' nullaway/src/main/java/com/uber/nullaway/dataflow/AccessPathNullnessPropagation.java
printf '%s\n' '--- repository guidance for inspected scopes ---'
cat /tmp/coderabbit-repo-knowledge/uber-nullaway-4b0a71dd/conventions/repo-wide.md
cat /tmp/coderabbit-repo-knowledge/uber-nullaway-4b0a71dd/learnings/nullaway-src-main-java-com-uber-nullaway-handler.md 2>/dev/null || trueRepository: uber/NullAway Length of output: 14234 🌐 Web query:
💡 Result: In the Checker Framework, the MethodInvocationNode class represents a method invocation within the Control Flow Graph (CFG) [1]. Its getArguments method is a simple accessor that returns the list of arguments associated with that invocation [2][1]. The underlying implementation relies on a protected field named arguments, which is defined as a List [1]. This list is initialized when the MethodInvocationNode is constructed [1]. The implementation of the getArguments method is as follows [1]: public List getArguments { return arguments; } Additionally, the class provides a getArgument(int i) method to retrieve a specific argument by its index, which is implemented as [1]: public Node getArgument(int i) { return arguments.get(i); } During the construction of a MethodInvocationNode, the arguments are passed in and stored in the class instance [1]. The constructor also iterates through these arguments to set their assignment contexts, specifically creating a MethodParameterContext for each argument based on the target method [1]. Citations:
Strengthen the varargs contract test
🤖 Prompt for AI AgentsSource: Coding guidelines |
||
|
|
||
| @Test | ||
| public void noContractCheckErrorsWithoutFlag() { | ||
| makeTestHelperWithArgs( | ||
|
|
@@ -479,6 +532,12 @@ class Test { | |
| @Contract("!null -> -> !null") | ||
| // BUG: Diagnostic contains: Invalid @Contract annotation | ||
| static @Nullable Object dontcare(@Nullable Object o) { return o; } | ||
| @Contract("!null -> !null") | ||
| // BUG: Diagnostic contains: Invalid @Contract annotation | ||
| static @Nullable Object tooManyConstraints() { return null; } | ||
| @Contract("true -> fail") | ||
| // BUG: Diagnostic contains: Invalid @Contract annotation | ||
| static void malformedFailContract() {} | ||
| // don't report errors about invalid contract annotations at calls to these methods | ||
| static Object test1() { | ||
| // BUG: Diagnostic contains: returning @Nullable expression | ||
|
|
@@ -492,6 +551,13 @@ static Object test3() { | |
| // BUG: Diagnostic contains: returning @Nullable expression | ||
| return baz(null); | ||
| } | ||
| static Object test4() { | ||
| // BUG: Diagnostic contains: returning @Nullable expression | ||
| return tooManyConstraints(); | ||
| } | ||
| static void test5() { | ||
| malformedFailContract(); | ||
| } | ||
| } | ||
| """) | ||
| .doTest(); | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.