mirror of
https://github.com/astral-sh/ruff.git
synced 2025-10-09 18:03:03 +00:00
[ty] Add diagnosis for function with no return statement but with return type annotation (#18359)
## Summary Partially implement https://github.com/astral-sh/ty/issues/538, ```py from pathlib import Path def setup_test_project(registry_name: str, registry_url: str, project_dir: str) -> Path: pyproject_file = Path(project_dir) / "pyproject.toml" pyproject_file.write_text("...", encoding="utf-8") ``` As no return statement is defined in the function `setup_test_project` with annotated return type `Path`, we provide the following diagnosis : - error[invalid-return-type]: Function **always** implicitly returns `None`, which is not assignable to return type `Path` with a subdiagnostic : - note: Consider changing your return annotation to `-> None` or adding a `return` statement ## Test Plan mdtests with snapshots to capture the subdiagnostic. I have to mention that existing snapshots were modified since they now fall in this category. --------- Co-authored-by: Carl Meyer <carl@astral.sh>
This commit is contained in:
parent
3445d1322d
commit
695de4f27f
8 changed files with 80 additions and 10 deletions
|
@ -1685,15 +1685,29 @@ pub(super) fn report_implicit_return_type(
|
|||
expected_ty: Type,
|
||||
has_empty_body: bool,
|
||||
enclosing_class_of_method: Option<ClassLiteral>,
|
||||
no_return: bool,
|
||||
) {
|
||||
let Some(builder) = context.report_lint(&INVALID_RETURN_TYPE, range) else {
|
||||
return;
|
||||
};
|
||||
let db = context.db();
|
||||
let mut diagnostic = builder.into_diagnostic(format_args!(
|
||||
"Function can implicitly return `None`, which is not assignable to return type `{}`",
|
||||
expected_ty.display(db)
|
||||
));
|
||||
|
||||
// If no return statement is defined in the function, then the function always returns `None`
|
||||
let mut diagnostic = if no_return {
|
||||
let mut diag = builder.into_diagnostic(format_args!(
|
||||
"Function always implicitly returns `None`, which is not assignable to return type `{}`",
|
||||
expected_ty.display(db),
|
||||
));
|
||||
diag.info(
|
||||
"Consider changing the return annotation to `-> None` or adding a `return` statement",
|
||||
);
|
||||
diag
|
||||
} else {
|
||||
builder.into_diagnostic(format_args!(
|
||||
"Function can implicitly return `None`, which is not assignable to return type `{}`",
|
||||
expected_ty.display(db),
|
||||
))
|
||||
};
|
||||
if !has_empty_body {
|
||||
return;
|
||||
}
|
||||
|
|
|
@ -1872,12 +1872,14 @@ impl<'db> TypeInferenceBuilder<'db> {
|
|||
if use_def.can_implicit_return(self.db())
|
||||
&& !Type::none(self.db()).is_assignable_to(self.db(), declared_ty)
|
||||
{
|
||||
let no_return = self.return_types_and_ranges.is_empty();
|
||||
report_implicit_return_type(
|
||||
&self.context,
|
||||
returns.range(),
|
||||
declared_ty,
|
||||
has_empty_body,
|
||||
enclosing_class_context,
|
||||
no_return,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue