Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}

}