-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: Add a diagnostic with a fix to remove return
at the end of functions
#11020
Closed
izik1
wants to merge
3
commits into
rust-lang:master
from
izik1:sr/add-remove-trailing-return-diagnostic
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
149 changes: 149 additions & 0 deletions
149
crates/ide_diagnostics/src/handlers/remove_trailing_return.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,149 @@ | ||
use hir::{db::AstDatabase, diagnostics::RemoveTrailingReturn, InFile}; | ||
use ide_db::{assists::Assist, source_change::SourceChange}; | ||
use syntax::{ast, AstNode}; | ||
use text_edit::TextEdit; | ||
|
||
use crate::{fix, Diagnostic, DiagnosticsContext, Severity}; | ||
|
||
// Diagnostic: remove-trailing-return | ||
// | ||
// This diagnostic is triggered when there is a redundant `return` at the end of a function. | ||
pub(crate) fn remove_trailing_return( | ||
ctx: &DiagnosticsContext<'_>, | ||
d: &RemoveTrailingReturn, | ||
) -> Diagnostic { | ||
Diagnostic::new( | ||
"remove-trailing-return", | ||
"replace return <expr>; with <expr>", | ||
ctx.sema.diagnostics_display_range(InFile::new(d.file, d.return_expr.clone().into())).range, | ||
) | ||
.severity(Severity::WeakWarning) | ||
.with_fixes(fixes(ctx, d)) | ||
} | ||
|
||
fn fixes(ctx: &DiagnosticsContext<'_>, d: &RemoveTrailingReturn) -> Option<Vec<Assist>> { | ||
let root = ctx.sema.db.parse_or_expand(d.file)?; | ||
|
||
let return_expr = d.return_expr.to_node(&root); | ||
|
||
let return_expr = ast::ReturnExpr::cast(return_expr.syntax().clone())?; | ||
|
||
let stmt = return_expr.syntax().parent().and_then(ast::ExprStmt::cast); | ||
let range_to_replace = match stmt { | ||
Some(stmt) => stmt.syntax().text_range(), | ||
None => return_expr.syntax().text_range(), | ||
}; | ||
|
||
let replacement = | ||
return_expr.expr().map_or_else(String::new, |expr| format!("{}", expr.syntax().text())); | ||
|
||
// this *seems* like a reasonable range to trigger in? | ||
let trigger_range = range_to_replace; | ||
|
||
let edit = TextEdit::replace(range_to_replace, replacement); | ||
|
||
let source_change = SourceChange::from_text_edit(d.file.original_file(ctx.sema.db), edit); | ||
|
||
Some(vec![fix( | ||
"replace_with_inner", | ||
"Replace return <expr>; with <expr>", | ||
source_change, | ||
trigger_range, | ||
)]) | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use crate::tests::{check_diagnostics, check_fix}; | ||
|
||
#[test] | ||
fn remove_trailing_return() { | ||
// fixme: this should include the semi. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hmm ye, this one is tricky as |
||
check_diagnostics( | ||
r#" | ||
fn foo() -> i8 { | ||
return 2; | ||
} //^^^^^^^^ 💡 weak: replace return <expr>; with <expr> | ||
"#, | ||
); | ||
} | ||
|
||
// fixme: implement this for lambdas and inner functions. | ||
#[test] | ||
fn remove_trailing_return_no_lambda() { | ||
// fixme: this should include the semi. | ||
check_diagnostics( | ||
r#" | ||
fn foo() -> i8 { | ||
let bar = || return 2; | ||
bar() | ||
} | ||
"#, | ||
); | ||
} | ||
|
||
#[test] | ||
fn remove_trailing_return_unit() { | ||
check_diagnostics( | ||
r#" | ||
fn foo() -> i8 { | ||
return | ||
} //^^^^^^ 💡 weak: replace return <expr>; with <expr> | ||
"#, | ||
); | ||
} | ||
|
||
#[test] | ||
fn remove_trailing_return_no_diagnostic_if_no_return_keyword() { | ||
check_diagnostics( | ||
r#" | ||
fn foo() -> i8 { | ||
3 | ||
} | ||
"#, | ||
); | ||
} | ||
|
||
#[test] | ||
fn remove_trailing_return_no_diagnostic_if_not_at_and() { | ||
check_diagnostics( | ||
r#" | ||
fn foo() -> i8 { | ||
if true { return 2; } | ||
3 | ||
} | ||
"#, | ||
); | ||
} | ||
|
||
#[test] | ||
fn replace_with_expr() { | ||
check_fix( | ||
r#" | ||
fn foo() -> i8 { | ||
return$0 2; | ||
} | ||
"#, | ||
r#" | ||
fn foo() -> i8 { | ||
2 | ||
} | ||
"#, | ||
); | ||
} | ||
#[test] | ||
fn replace_with_unit() { | ||
check_fix( | ||
r#" | ||
fn foo() { | ||
return$0/*ensure tidy is happy*/ | ||
} | ||
"#, | ||
r#" | ||
fn foo() { | ||
/*ensure tidy is happy*/ | ||
} | ||
"#, | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Inner functions I believe should already be covered by this as they are their own
DefWithBodyId
again.For lambdas you will just have to go iterate through all expressions of the body and search for a lambda expression, then do the logic you already do here for it's body expression again