fix tests

This commit is contained in:
Cameron 2022-10-22 06:23:23 +01:00
parent 5b56d7bd90
commit 3dd2f99c49
6 changed files with 117 additions and 6 deletions

View file

@ -2857,6 +2857,13 @@ impl Type {
matches!(self.ty.kind(Interner), TyKind::Scalar(Scalar::Uint(UintTy::Usize))) matches!(self.ty.kind(Interner), TyKind::Scalar(Scalar::Uint(UintTy::Usize)))
} }
pub fn is_int_or_uint(&self) -> bool {
match self.ty.kind(Interner) {
TyKind::Scalar(Scalar::Int(_) | Scalar::Uint(_)) => true,
_ => false,
}
}
pub fn remove_ref(&self) -> Option<Type> { pub fn remove_ref(&self) -> Option<Type> {
match &self.ty.kind(Interner) { match &self.ty.kind(Interner) {
TyKind::Ref(.., ty) => Some(self.derived(ty.clone())), TyKind::Ref(.., ty) => Some(self.derived(ty.clone())),

View file

@ -1,10 +1,22 @@
use crate::assist_context::{AssistContext, Assists}; use crate::assist_context::{AssistContext, Assists};
use crate::utils::{replace_arith, ArithKind}; use crate::utils::{replace_arith, ArithKind};
pub(crate) fn replace_arith_with_checked( // Assist: replace_arith_with_checked
acc: &mut Assists, //
ctx: &AssistContext<'_>, // Replaces arithmetic on integers with the `checked_*` equivalent.
) -> Option<()> { //
// ```
// fn main() {
// let x = 1 $0+ 2;
// }
// ```
// ->
// ```
// fn main() {
// let x = 1.checked_add(2);
// }
// ```
pub(crate) fn replace_arith_with_checked(acc: &mut Assists, ctx: &AssistContext<'_>) -> Option<()> {
replace_arith(acc, ctx, ArithKind::Checked) replace_arith(acc, ctx, ArithKind::Checked)
} }

View file

@ -1,6 +1,21 @@
use crate::assist_context::{AssistContext, Assists}; use crate::assist_context::{AssistContext, Assists};
use crate::utils::{replace_arith, ArithKind}; use crate::utils::{replace_arith, ArithKind};
// Assist: replace_arith_with_saturating
//
// Replaces arithmetic on integers with the `saturating_*` equivalent.
//
// ```
// fn main() {
// let x = 1 $0+ 2;
// }
// ```
// ->
// ```
// fn main() {
// let x = 1.saturating_add(2);
// }
// ```
pub(crate) fn replace_arith_with_saturating( pub(crate) fn replace_arith_with_saturating(
acc: &mut Assists, acc: &mut Assists,
ctx: &AssistContext<'_>, ctx: &AssistContext<'_>,

View file

@ -1,6 +1,21 @@
use crate::assist_context::{AssistContext, Assists}; use crate::assist_context::{AssistContext, Assists};
use crate::utils::{replace_arith, ArithKind}; use crate::utils::{replace_arith, ArithKind};
// Assist: replace_arith_with_wrapping
//
// Replaces arithmetic on integers with the `wrapping_*` equivalent.
//
// ```
// fn main() {
// let x = 1 $0+ 2;
// }
// ```
// ->
// ```
// fn main() {
// let x = 1.wrapping_add(2);
// }
// ```
pub(crate) fn replace_arith_with_wrapping( pub(crate) fn replace_arith_with_wrapping(
acc: &mut Assists, acc: &mut Assists,
ctx: &AssistContext<'_>, ctx: &AssistContext<'_>,

View file

@ -1978,6 +1978,57 @@ impl Foo for Bar {
) )
} }
#[test]
fn doctest_replace_arith_with_checked() {
check_doc_test(
"replace_arith_with_checked",
r#####"
fn main() {
let x = 1 $0+ 2;
}
"#####,
r#####"
fn main() {
let x = 1.checked_add(2);
}
"#####,
)
}
#[test]
fn doctest_replace_arith_with_saturating() {
check_doc_test(
"replace_arith_with_saturating",
r#####"
fn main() {
let x = 1 $0+ 2;
}
"#####,
r#####"
fn main() {
let x = 1.saturating_add(2);
}
"#####,
)
}
#[test]
fn doctest_replace_arith_with_wrapping() {
check_doc_test(
"replace_arith_with_wrapping",
r#####"
fn main() {
let x = 1 $0+ 2;
}
"#####,
r#####"
fn main() {
let x = 1.wrapping_add(2);
}
"#####,
)
}
#[test] #[test]
fn doctest_replace_char_with_string() { fn doctest_replace_char_with_string() {
check_doc_test( check_doc_test(

View file

@ -722,8 +722,8 @@ impl ArithKind {
fn assist_id(&self) -> AssistId { fn assist_id(&self) -> AssistId {
let s = match self { let s = match self {
ArithKind::Saturating => "replace_arith_with_saturating", ArithKind::Saturating => "replace_arith_with_saturating",
ArithKind::Checked => "replace_arith_with_saturating", ArithKind::Checked => "replace_arith_with_checked",
ArithKind::Wrapping => "replace_arith_with_saturating", ArithKind::Wrapping => "replace_arith_with_wrapping",
}; };
AssistId(s, AssistKind::RefactorRewrite) AssistId(s, AssistKind::RefactorRewrite)
@ -771,6 +771,10 @@ pub(crate) fn replace_arith(
) -> Option<()> { ) -> Option<()> {
let (lhs, op, rhs) = parse_binary_op(ctx)?; let (lhs, op, rhs) = parse_binary_op(ctx)?;
if !is_primitive_int(ctx, &lhs) || !is_primitive_int(ctx, &rhs) {
return None;
}
let start = lhs.syntax().text_range().start(); let start = lhs.syntax().text_range().start();
let end = rhs.syntax().text_range().end(); let end = rhs.syntax().text_range().end();
let range = TextRange::new(start, end); let range = TextRange::new(start, end);
@ -782,6 +786,13 @@ pub(crate) fn replace_arith(
}) })
} }
fn is_primitive_int(ctx: &AssistContext<'_>, expr: &Expr) -> bool {
match ctx.sema.type_of_expr(expr) {
Some(ty) => ty.adjusted().is_int_or_uint(),
_ => false,
}
}
/// Extract the operands of an arithmetic expression (e.g. `1 + 2` or `1.checked_add(2)`) /// Extract the operands of an arithmetic expression (e.g. `1 + 2` or `1.checked_add(2)`)
fn parse_binary_op(ctx: &AssistContext<'_>) -> Option<(Expr, ArithOp, Expr)> { fn parse_binary_op(ctx: &AssistContext<'_>) -> Option<(Expr, ArithOp, Expr)> {
let expr = ctx.find_node_at_offset::<BinExpr>()?; let expr = ctx.find_node_at_offset::<BinExpr>()?;