Skip to content

Commit 95e928e

Browse files
committed
Add option to allow returning nullable from methods in
Jsr305AnnotationCheck #887
1 parent f3b1a08 commit 95e928e

File tree

2 files changed

+45
-1
lines changed

2 files changed

+45
-1
lines changed

sevntu-checks/src/main/java/com/github/sevntu/checkstyle/checks/coding/Jsr305AnnotationsCheck.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,8 @@ private enum NullnessAnnotation {
329329
private boolean allowOverridingReturnValue;
330330
/** Parameter: overriding parameter annotations allowed. */
331331
private boolean allowOverridingParameter;
332+
/** Parameter: allow nullable return value. */
333+
private boolean allowNullableReturnValue;
332334

333335
/** State, is a package excluded. */
334336
private boolean packageExcluded;
@@ -415,6 +417,16 @@ public void setAllowOverridingParameter(final boolean newAllowOverridingParamete
415417
allowOverridingParameter = newAllowOverridingParameter;
416418
}
417419

420+
/**
421+
* Sets the property for allowing nullable return values.
422+
*
423+
* @param newAllowNullableReturnValue
424+
* true if yes
425+
*/
426+
public void setAllowNullableReturnValue(final boolean newAllowNullableReturnValue) {
427+
allowNullableReturnValue = newAllowNullableReturnValue;
428+
}
429+
418430
/**
419431
* Maps annotations to their respective names.
420432
*
@@ -771,8 +783,10 @@ protected MethodJsr305Handler(final DetailAST ast) {
771783
protected void runReturnAnnotationHandler() {
772784
checkContainsAny(MSG_RETURN_VALUE_WITH_NONNULL_BY_DEFAULT,
773785
NullnessAnnotation.RETURN_VALUES_ARE_NONNULL_BY_DEFAULT);
774-
checkContainsAny(MSG_RETURN_VALUE_WITH_NULLABLE,
786+
if (!allowNullableReturnValue) {
787+
checkContainsAny(MSG_RETURN_VALUE_WITH_NULLABLE,
775788
NullnessAnnotation.NULLABLE);
789+
}
776790
checkContainsAll(MSG_CONTRADICTING_RETURN_VALUE_ANNOTATIONS, NullnessAnnotation.NONNULL,
777791
NullnessAnnotation.CHECK_FOR_NULL);
778792
checkContainsAll(MSG_OVERRIDDEN_METHOD_WITH_CHECK_RETURN_VALUE,

sevntu-checks/src/test/java/com/github/sevntu/checkstyle/checks/coding/Jsr305AnnotationsCheckTest.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -407,4 +407,34 @@ public void testNestedAnnotations() throws Exception {
407407
expected);
408408
}
409409

410+
@Test
411+
public void testAllowNullableReturn() throws Exception {
412+
final DefaultConfiguration checkConfig = createModuleConfig(Jsr305AnnotationsCheck.class);
413+
checkConfig.addAttribute("packages", "com.github.sevntu.checkstyle.checks.coding");
414+
checkConfig.addAttribute("allowNullableReturnValue", "true");
415+
416+
final String[] expected = {
417+
"51:5: " + getCheckMessage(
418+
Jsr305AnnotationsCheck.MSG_CONTRADICTING_RETURN_VALUE_ANNOTATIONS, "e"),
419+
"69:5: " + getCheckMessage(
420+
Jsr305AnnotationsCheck.MSG_PRIMITIVES_WITH_NULLNESS_ANNOTATION, "e"),
421+
"75:5: " + getCheckMessage(
422+
Jsr305AnnotationsCheck.MSG_PRIMITIVES_WITH_NULLNESS_ANNOTATION, "e"),
423+
"92:5: " + getCheckMessage(
424+
Jsr305AnnotationsCheck.MSG_CONTRADICTING_CLASS_LEVEL_ANNOTATIONS, "e"),
425+
"95:26: " + getCheckMessage(
426+
Jsr305AnnotationsCheck.MSG_PARAMETER_WITHOUT_NULLNESS_ANNOTATION, "e"),
427+
"95:48: " + getCheckMessage(
428+
Jsr305AnnotationsCheck.MSG_PARAMETER_WITHOUT_NULLNESS_ANNOTATION, "e"),
429+
"99:5: " + getCheckMessage(
430+
Jsr305AnnotationsCheck.MSG_OVERRIDDEN_METHOD_WITH_CHECK_RETURN_VALUE, "e"),
431+
"105:5: " + getCheckMessage(
432+
Jsr305AnnotationsCheck.MSG_VOID_WITH_CHECK_RETURN_VALUE_ANNOTATION, "e"),
433+
"110:5: " + getCheckMessage(
434+
Jsr305AnnotationsCheck.MSG_PRIMITIVES_WITH_NULLNESS_ANNOTATION, "e"),
435+
};
436+
437+
verify(checkConfig, getPath("InputJsr305AnnotationsCheckWithReturnValue.java"), expected);
438+
}
439+
410440
}

0 commit comments

Comments
 (0)