Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 3dd2f99

Browse files
committed
fix tests
1 parent 5b56d7b commit 3dd2f99

File tree

6 files changed

+117
-6
lines changed

6 files changed

+117
-6
lines changed

crates/hir/src/lib.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2857,6 +2857,13 @@ impl Type {
28572857
matches!(self.ty.kind(Interner), TyKind::Scalar(Scalar::Uint(UintTy::Usize)))
28582858
}
28592859

2860+
pub fn is_int_or_uint(&self) -> bool {
2861+
match self.ty.kind(Interner) {
2862+
TyKind::Scalar(Scalar::Int(_) | Scalar::Uint(_)) => true,
2863+
_ => false,
2864+
}
2865+
}
2866+
28602867
pub fn remove_ref(&self) -> Option<Type> {
28612868
match &self.ty.kind(Interner) {
28622869
TyKind::Ref(.., ty) => Some(self.derived(ty.clone())),

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

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,22 @@
11
use crate::assist_context::{AssistContext, Assists};
22
use crate::utils::{replace_arith, ArithKind};
33

4-
pub(crate) fn replace_arith_with_checked(
5-
acc: &mut Assists,
6-
ctx: &AssistContext<'_>,
7-
) -> Option<()> {
4+
// Assist: replace_arith_with_checked
5+
//
6+
// Replaces arithmetic on integers with the `checked_*` equivalent.
7+
//
8+
// ```
9+
// fn main() {
10+
// let x = 1 $0+ 2;
11+
// }
12+
// ```
13+
// ->
14+
// ```
15+
// fn main() {
16+
// let x = 1.checked_add(2);
17+
// }
18+
// ```
19+
pub(crate) fn replace_arith_with_checked(acc: &mut Assists, ctx: &AssistContext<'_>) -> Option<()> {
820
replace_arith(acc, ctx, ArithKind::Checked)
921
}
1022

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,21 @@
11
use crate::assist_context::{AssistContext, Assists};
22
use crate::utils::{replace_arith, ArithKind};
33

4+
// Assist: replace_arith_with_saturating
5+
//
6+
// Replaces arithmetic on integers with the `saturating_*` equivalent.
7+
//
8+
// ```
9+
// fn main() {
10+
// let x = 1 $0+ 2;
11+
// }
12+
// ```
13+
// ->
14+
// ```
15+
// fn main() {
16+
// let x = 1.saturating_add(2);
17+
// }
18+
// ```
419
pub(crate) fn replace_arith_with_saturating(
520
acc: &mut Assists,
621
ctx: &AssistContext<'_>,

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,21 @@
11
use crate::assist_context::{AssistContext, Assists};
22
use crate::utils::{replace_arith, ArithKind};
33

4+
// Assist: replace_arith_with_wrapping
5+
//
6+
// Replaces arithmetic on integers with the `wrapping_*` equivalent.
7+
//
8+
// ```
9+
// fn main() {
10+
// let x = 1 $0+ 2;
11+
// }
12+
// ```
13+
// ->
14+
// ```
15+
// fn main() {
16+
// let x = 1.wrapping_add(2);
17+
// }
18+
// ```
419
pub(crate) fn replace_arith_with_wrapping(
520
acc: &mut Assists,
621
ctx: &AssistContext<'_>,

crates/ide-assists/src/tests/generated.rs

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1978,6 +1978,57 @@ impl Foo for Bar {
19781978
)
19791979
}
19801980

1981+
#[test]
1982+
fn doctest_replace_arith_with_checked() {
1983+
check_doc_test(
1984+
"replace_arith_with_checked",
1985+
r#####"
1986+
fn main() {
1987+
let x = 1 $0+ 2;
1988+
}
1989+
"#####,
1990+
r#####"
1991+
fn main() {
1992+
let x = 1.checked_add(2);
1993+
}
1994+
"#####,
1995+
)
1996+
}
1997+
1998+
#[test]
1999+
fn doctest_replace_arith_with_saturating() {
2000+
check_doc_test(
2001+
"replace_arith_with_saturating",
2002+
r#####"
2003+
fn main() {
2004+
let x = 1 $0+ 2;
2005+
}
2006+
"#####,
2007+
r#####"
2008+
fn main() {
2009+
let x = 1.saturating_add(2);
2010+
}
2011+
"#####,
2012+
)
2013+
}
2014+
2015+
#[test]
2016+
fn doctest_replace_arith_with_wrapping() {
2017+
check_doc_test(
2018+
"replace_arith_with_wrapping",
2019+
r#####"
2020+
fn main() {
2021+
let x = 1 $0+ 2;
2022+
}
2023+
"#####,
2024+
r#####"
2025+
fn main() {
2026+
let x = 1.wrapping_add(2);
2027+
}
2028+
"#####,
2029+
)
2030+
}
2031+
19812032
#[test]
19822033
fn doctest_replace_char_with_string() {
19832034
check_doc_test(

crates/ide-assists/src/utils.rs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -722,8 +722,8 @@ impl ArithKind {
722722
fn assist_id(&self) -> AssistId {
723723
let s = match self {
724724
ArithKind::Saturating => "replace_arith_with_saturating",
725-
ArithKind::Checked => "replace_arith_with_saturating",
726-
ArithKind::Wrapping => "replace_arith_with_saturating",
725+
ArithKind::Checked => "replace_arith_with_checked",
726+
ArithKind::Wrapping => "replace_arith_with_wrapping",
727727
};
728728

729729
AssistId(s, AssistKind::RefactorRewrite)
@@ -771,6 +771,10 @@ pub(crate) fn replace_arith(
771771
) -> Option<()> {
772772
let (lhs, op, rhs) = parse_binary_op(ctx)?;
773773

774+
if !is_primitive_int(ctx, &lhs) || !is_primitive_int(ctx, &rhs) {
775+
return None;
776+
}
777+
774778
let start = lhs.syntax().text_range().start();
775779
let end = rhs.syntax().text_range().end();
776780
let range = TextRange::new(start, end);
@@ -782,6 +786,13 @@ pub(crate) fn replace_arith(
782786
})
783787
}
784788

789+
fn is_primitive_int(ctx: &AssistContext<'_>, expr: &Expr) -> bool {
790+
match ctx.sema.type_of_expr(expr) {
791+
Some(ty) => ty.adjusted().is_int_or_uint(),
792+
_ => false,
793+
}
794+
}
795+
785796
/// Extract the operands of an arithmetic expression (e.g. `1 + 2` or `1.checked_add(2)`)
786797
fn parse_binary_op(ctx: &AssistContext<'_>) -> Option<(Expr, ArithOp, Expr)> {
787798
let expr = ctx.find_node_at_offset::<BinExpr>()?;

0 commit comments

Comments
 (0)