Skip to content

Commit 3536955

Browse files
Merge pull request #20512 from A4-Tacks/arith-op-not-on-selected
replace_arith_op not applicable on selected
2 parents 544ef84 + 0fe2d7f commit 3536955

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed

crates/ide-assists/src/handlers/replace_arith_op.rs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,9 @@ fn is_primitive_int(ctx: &AssistContext<'_>, expr: &ast::Expr) -> bool {
102102

103103
/// Extract the operands of an arithmetic expression (e.g. `1 + 2` or `1.checked_add(2)`)
104104
fn parse_binary_op(ctx: &AssistContext<'_>) -> Option<(ast::Expr, ArithOp, ast::Expr)> {
105+
if !ctx.has_empty_selection() {
106+
return None;
107+
}
105108
let expr = ctx.find_node_at_offset::<ast::BinExpr>()?;
106109

107110
let op = match expr.op_kind() {
@@ -163,7 +166,7 @@ impl ArithKind {
163166

164167
#[cfg(test)]
165168
mod tests {
166-
use crate::tests::check_assist;
169+
use crate::tests::{check_assist, check_assist_not_applicable};
167170

168171
use super::*;
169172

@@ -220,6 +223,18 @@ fn main() {
220223
fn main() {
221224
let x = 1.wrapping_add(2);
222225
}
226+
"#,
227+
)
228+
}
229+
230+
#[test]
231+
fn replace_arith_not_applicable_with_non_empty_selection() {
232+
check_assist_not_applicable(
233+
replace_arith_with_checked,
234+
r#"
235+
fn main() {
236+
let x = 1 $0+$0 2;
237+
}
223238
"#,
224239
)
225240
}

0 commit comments

Comments
 (0)