[ty] Add subdiagnostic about empty bodies in more cases (#18942)

This commit is contained in:
Alex Waygood 2025-06-25 20:25:00 +01:00 committed by GitHub
parent 5d546c600a
commit c77e72ea1a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 5 additions and 4 deletions

View file

@ -93,6 +93,7 @@ error[invalid-return-type]: Function always implicitly returns `None`, which is
| ^ | ^
| |
info: Consider changing the return annotation to `-> None` or adding a `return` statement info: Consider changing the return annotation to `-> None` or adding a `return` statement
info: Only functions in stub files, methods on protocol classes, or methods with `@abstractmethod` are permitted to have empty bodies
info: rule `invalid-return-type` is enabled by default info: rule `invalid-return-type` is enabled by default
``` ```

View file

@ -1828,14 +1828,14 @@ pub(super) fn report_implicit_return_type(
if !has_empty_body { if !has_empty_body {
return; return;
} }
diagnostic.info(
"Only functions in stub files, methods on protocol classes, \
or methods with `@abstractmethod` are permitted to have empty bodies",
);
let Some(class) = enclosing_class_of_method else { let Some(class) = enclosing_class_of_method else {
return; return;
}; };
if class.iter_mro(db, None).contains(&ClassBase::Protocol) { if class.iter_mro(db, None).contains(&ClassBase::Protocol) {
diagnostic.info(
"Only functions in stub files, methods on protocol classes, \
or methods with `@abstractmethod` are permitted to have empty bodies",
);
diagnostic.info(format_args!( diagnostic.info(format_args!(
"Class `{}` has `typing.Protocol` in its MRO, but it is not a protocol class", "Class `{}` has `typing.Protocol` in its MRO, but it is not a protocol class",
class.name(db) class.name(db)