mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-25 11:17:13 +00:00
Merge pull request #20723 from A4-Tacks/fix-bind-unused-applicable-underscore
Fix applicable on underscore for bind_unused_param
This commit is contained in:
commit
b465ae054a
@ -2,7 +2,7 @@ use crate::assist_context::{AssistContext, Assists};
|
||||
use ide_db::{LineIndexDatabase, assists::AssistId, defs::Definition};
|
||||
use syntax::{
|
||||
AstNode,
|
||||
ast::{self, edit_in_place::Indent},
|
||||
ast::{self, HasName, edit_in_place::Indent},
|
||||
};
|
||||
|
||||
// Assist: bind_unused_param
|
||||
@ -22,6 +22,7 @@ pub(crate) fn bind_unused_param(acc: &mut Assists, ctx: &AssistContext<'_>) -> O
|
||||
let param: ast::Param = ctx.find_node_at_offset()?;
|
||||
|
||||
let Some(ast::Pat::IdentPat(ident_pat)) = param.pat() else { return None };
|
||||
let name = ident_pat.name().filter(|n| !n.text().starts_with('_'))?;
|
||||
|
||||
let param_def = {
|
||||
let local = ctx.sema.to_def(&ident_pat)?;
|
||||
@ -39,14 +40,14 @@ pub(crate) fn bind_unused_param(acc: &mut Assists, ctx: &AssistContext<'_>) -> O
|
||||
|
||||
acc.add(
|
||||
AssistId::quick_fix("bind_unused_param"),
|
||||
format!("Bind as `let _ = {ident_pat};`"),
|
||||
format!("Bind as `let _ = {name};`"),
|
||||
param.syntax().text_range(),
|
||||
|builder| {
|
||||
let line_index = ctx.db().line_index(ctx.vfs_file_id());
|
||||
|
||||
let indent = func.indent_level();
|
||||
let text_indent = indent + 1;
|
||||
let mut text = format!("\n{text_indent}let _ = {ident_pat};");
|
||||
let mut text = format!("\n{text_indent}let _ = {name};");
|
||||
|
||||
let left_line = line_index.line_col(l_curly_range.end()).line;
|
||||
let right_line = line_index.line_col(r_curly_range.start()).line;
|
||||
@ -83,6 +84,22 @@ fn foo(y: i32) {
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn bind_unused_ref_ident_pat() {
|
||||
cov_mark::check!(single_line);
|
||||
check_assist(
|
||||
bind_unused_param,
|
||||
r#"
|
||||
fn foo(ref $0y: i32) {}
|
||||
"#,
|
||||
r#"
|
||||
fn foo(ref y: i32) {
|
||||
let _ = y;
|
||||
}
|
||||
"#,
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn bind_unused_empty_block_with_newline() {
|
||||
check_assist(
|
||||
@ -149,6 +166,16 @@ impl Trait for () {
|
||||
bind_unused_param,
|
||||
r#"
|
||||
fn foo(x: i32, $0y: i32) { y; }
|
||||
"#,
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn keep_underscore_used() {
|
||||
check_assist_not_applicable(
|
||||
bind_unused_param,
|
||||
r#"
|
||||
fn foo($0_x: i32, y: i32) {}
|
||||
"#,
|
||||
);
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user