Skip to content
Merged
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 @@ -74,6 +74,13 @@ public void testSubscribeEventReturnTypeCancellable() {
""");
assertThat(compilation).hadWarningContaining("consider using a void return type");

compilation = compile("""
@SubscribeEvent
boolean alwaysCancellingEvent(CancelableEvent event) { return true; }
""");
assertThat(compilation).hadWarningContaining("consider using a void return type");
assertThat(compilation).hadWarningContaining("alwaysCancelling = true");

compilation = compile("""
@SubscribeEvent(alwaysCancelling = true)
void alwaysCancellingEvent(CancelableEvent event) {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,12 +104,20 @@ private void validate(ExecutableElement method) {
var scanner = new ReturnScanner();
scanner.scan(methodPath, null);

if (scanner.sawReturn && !scanner.sawNonLiteralReturn && scanner.sawLiteralFalse && !scanner.sawLiteralTrue) {
// if we get here, all returns in this method are literal `return false;` statements
processingEnv.getMessager().printWarning(
"Listener always returns false, consider using a void return type instead",
method
);
if (scanner.sawReturn && !scanner.sawNonLiteralReturn) {
if (scanner.sawLiteralFalse && !scanner.sawLiteralTrue) {
// if we get here, all returns in this method are literal `return false;` statements
processingEnv.getMessager().printWarning(
"Listener always returns false, consider using a void return type instead",
method
);
} else if (scanner.sawLiteralTrue && !scanner.sawLiteralFalse) {
// if we get here, all returns in this method are literal `return true;` statements
processingEnv.getMessager().printWarning(
"Listener always returns true, consider using a void return type combined with @SubscribeEvent(alwaysCancelling = true) or CancellableEventBus#addListener(true, ...) instead",
method
);
}
}
}
}
Expand Down