Skip to content

Commit

Permalink
Add option to allow returning nullable from methods in
Browse files Browse the repository at this point in the history
Jsr305AnnotationCheck #887
  • Loading branch information
vdaniloff committed Apr 11, 2022
1 parent f3b1a08 commit 95e928e
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,8 @@ private enum NullnessAnnotation {
private boolean allowOverridingReturnValue;
/** Parameter: overriding parameter annotations allowed. */
private boolean allowOverridingParameter;
/** Parameter: allow nullable return value. */
private boolean allowNullableReturnValue;

/** State, is a package excluded. */
private boolean packageExcluded;
Expand Down Expand Up @@ -415,6 +417,16 @@ public void setAllowOverridingParameter(final boolean newAllowOverridingParamete
allowOverridingParameter = newAllowOverridingParameter;
}

/**
* Sets the property for allowing nullable return values.
*
* @param newAllowNullableReturnValue
* true if yes
*/
public void setAllowNullableReturnValue(final boolean newAllowNullableReturnValue) {
allowNullableReturnValue = newAllowNullableReturnValue;
}

/**
* Maps annotations to their respective names.
*
Expand Down Expand Up @@ -771,8 +783,10 @@ protected MethodJsr305Handler(final DetailAST ast) {
protected void runReturnAnnotationHandler() {
checkContainsAny(MSG_RETURN_VALUE_WITH_NONNULL_BY_DEFAULT,
NullnessAnnotation.RETURN_VALUES_ARE_NONNULL_BY_DEFAULT);
checkContainsAny(MSG_RETURN_VALUE_WITH_NULLABLE,
if (!allowNullableReturnValue) {
checkContainsAny(MSG_RETURN_VALUE_WITH_NULLABLE,
NullnessAnnotation.NULLABLE);
}
checkContainsAll(MSG_CONTRADICTING_RETURN_VALUE_ANNOTATIONS, NullnessAnnotation.NONNULL,
NullnessAnnotation.CHECK_FOR_NULL);
checkContainsAll(MSG_OVERRIDDEN_METHOD_WITH_CHECK_RETURN_VALUE,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -407,4 +407,34 @@ public void testNestedAnnotations() throws Exception {
expected);
}

@Test
public void testAllowNullableReturn() throws Exception {
final DefaultConfiguration checkConfig = createModuleConfig(Jsr305AnnotationsCheck.class);
checkConfig.addAttribute("packages", "com.github.sevntu.checkstyle.checks.coding");
checkConfig.addAttribute("allowNullableReturnValue", "true");

final String[] expected = {
"51:5: " + getCheckMessage(
Jsr305AnnotationsCheck.MSG_CONTRADICTING_RETURN_VALUE_ANNOTATIONS, "e"),
"69:5: " + getCheckMessage(
Jsr305AnnotationsCheck.MSG_PRIMITIVES_WITH_NULLNESS_ANNOTATION, "e"),
"75:5: " + getCheckMessage(
Jsr305AnnotationsCheck.MSG_PRIMITIVES_WITH_NULLNESS_ANNOTATION, "e"),
"92:5: " + getCheckMessage(
Jsr305AnnotationsCheck.MSG_CONTRADICTING_CLASS_LEVEL_ANNOTATIONS, "e"),
"95:26: " + getCheckMessage(
Jsr305AnnotationsCheck.MSG_PARAMETER_WITHOUT_NULLNESS_ANNOTATION, "e"),
"95:48: " + getCheckMessage(
Jsr305AnnotationsCheck.MSG_PARAMETER_WITHOUT_NULLNESS_ANNOTATION, "e"),
"99:5: " + getCheckMessage(
Jsr305AnnotationsCheck.MSG_OVERRIDDEN_METHOD_WITH_CHECK_RETURN_VALUE, "e"),
"105:5: " + getCheckMessage(
Jsr305AnnotationsCheck.MSG_VOID_WITH_CHECK_RETURN_VALUE_ANNOTATION, "e"),
"110:5: " + getCheckMessage(
Jsr305AnnotationsCheck.MSG_PRIMITIVES_WITH_NULLNESS_ANNOTATION, "e"),
};

verify(checkConfig, getPath("InputJsr305AnnotationsCheckWithReturnValue.java"), expected);
}

}

0 comments on commit 95e928e

Please sign in to comment.