-
Notifications
You must be signed in to change notification settings - Fork 19
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
Adding notcheckdeadcode option #633
base: master
Are you sure you want to change the base?
Changes from all commits
7102456
bd2e5e7
58f4773
1d1c348
2540cd3
02c69a8
a3e4013
73de62f
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 |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package org.checkerframework.checker.test.junit; | ||
|
||
import org.checkerframework.framework.test.CheckerFrameworkPerDirectoryTest; | ||
import org.junit.runners.Parameterized; | ||
|
||
import java.io.File; | ||
import java.util.List; | ||
|
||
/** | ||
* JUnit tests for the Nullness checker when -AignoreCheckDeadCode command-line argument is used. | ||
*/ | ||
public class NullnessDeadBranchTest extends CheckerFrameworkPerDirectoryTest { | ||
/** | ||
* Create a NullnessNullMarkedTest. | ||
* | ||
* @param testFiles the files containing test code, which will be type-checked | ||
*/ | ||
public NullnessDeadBranchTest(List<File> testFiles) { | ||
super( | ||
testFiles, | ||
org.checkerframework.checker.nullness.NullnessChecker.class, | ||
"nullness-deadbranch", | ||
"-AignoreCheckDeadCode"); | ||
} | ||
|
||
@Parameterized.Parameters | ||
public static String[] getTestDirs() { | ||
return new String[] {"nullness-deadbranch"}; | ||
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. How about |
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
// @skip-test until the bug is fixed | ||
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. Do these tests not work? |
||
|
||
class DeadBranchNPE { | ||
void test1() { | ||
Object obj = null; | ||
if (true) { | ||
// :: error: (dereference.of.nullable) | ||
obj.toString(); | ||
} else { | ||
obj.toString(); | ||
} | ||
} | ||
|
||
void test2() { | ||
Object obj = null; | ||
// :: error: (dereference.of.nullable) | ||
obj.toString(); | ||
for (int i = 0; i < 0; i++) { | ||
obj.toString(); | ||
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. Add a comment to each dead branch why they are dead. |
||
} | ||
} | ||
|
||
void test3() { | ||
Object obj = null; | ||
// :: error: (dereference.of.nullable) | ||
obj.toString(); | ||
while (obj != null) { | ||
obj.toString(); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,9 @@ Version 3.40.0-eisop3 (November ??, 2023) | |
|
||
**User-visible changes:** | ||
|
||
Add a new command-line argument `-AignoreCheckDeadCode` disables the checker for code in dead expression. | ||
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. When you update the branch, make sure to move this to the next upcoming release. 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. And try to make this sentence easier to read. |
||
This option is not enabled by default. | ||
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. Please also add documentation in the manual, where we have a short paragraph for each option. |
||
|
||
**Implementation details:** | ||
|
||
**Closed issues:** | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -286,6 +286,9 @@ public class BaseTypeVisitor<Factory extends GenericAnnotatedTypeFactory<?, ?, ? | |
/** True if "-AcheckEnclosingExpr" was passed on the command line. */ | ||
private final boolean checkEnclosingExpr; | ||
|
||
/** True if "-AigoreCheckDeadCode" was passed on the command line. */ | ||
protected final boolean ignoreCheckDeadCode; | ||
|
||
/** The tree of the enclosing method that is currently being visited. */ | ||
protected @Nullable MethodTree methodTree = null; | ||
|
||
|
@@ -346,6 +349,7 @@ protected BaseTypeVisitor(BaseTypeChecker checker, @Nullable Factory typeFactory | |
checkCastElementType = checker.hasOption("checkCastElementType"); | ||
conservativeUninferredTypeArguments = | ||
checker.hasOption("conservativeUninferredTypeArguments"); | ||
ignoreCheckDeadCode = checker.hasOption("ignoreCheckDeadCode"); | ||
} | ||
|
||
/** An array containing just {@code BaseTypeChecker.class}. */ | ||
|
@@ -5181,10 +5185,12 @@ protected TypeValidator createTypeValidator() { | |
* @return true if checker should not test exprTree | ||
*/ | ||
protected final boolean shouldSkipUses(ExpressionTree exprTree) { | ||
// System.out.printf("shouldSkipUses: %s: %s%n", exprTree.getClass(), exprTree); | ||
// if (atypeFactory.isUnreachable(exprTree)) { | ||
// return true; | ||
// } | ||
if (ignoreCheckDeadCode) { | ||
System.out.printf("shouldSkipUses: %s: %s%n", exprTree.getClass(), exprTree); | ||
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. This is debugging output and should remain a comment. 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. Also, the debugging output shouldn't be guarded by the new condition. |
||
if (atypeFactory.isUnreachable(exprTree)) { | ||
return true; | ||
} | ||
} | ||
Element elm = TreeUtils.elementFromTree(exprTree); | ||
return checker.shouldSkipUses(elm); | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -150,6 +150,10 @@ | |
// org.checkerframework.framework.source.SourceChecker.report | ||
"warns", | ||
|
||
// Make checker ignore the expression in dead branch | ||
// org.checkerframework.framework.common.basetype.BaseTypeVisitor.shouldSkipUses | ||
"ignoreCheckDeadCode", | ||
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. As noted in #633 (comment) |
||
|
||
/// | ||
/// More sound (strict checking): enable errors that are disabled by default | ||
/// | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why make these two separate 'if's?