Skip to content

Commit effe235

Browse files
authored
Fix expect_fun_call suggests wrongly for string slicing (#16752)
Closes #16747 changelog: [`expect_fun_call`] fix wrong suggestions for string slicing
2 parents ded0118 + e1b713a commit effe235

4 files changed

Lines changed: 40 additions & 2 deletions

File tree

clippy_lints/src/methods/expect_fun_call.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,13 @@ fn get_arg_root<'a>(cx: &LateContext<'_>, arg: &'a hir::Expr<'a>) -> &'a hir::Ex
7575
let mut arg_root = peel_blocks(arg);
7676
loop {
7777
arg_root = match &arg_root.kind {
78-
hir::ExprKind::AddrOf(hir::BorrowKind::Ref, _, expr) => expr,
78+
hir::ExprKind::AddrOf(hir::BorrowKind::Ref, _, expr) => {
79+
let expr_ty = cx.typeck_results().expr_ty(expr);
80+
if expr_ty.is_str() {
81+
break;
82+
}
83+
expr
84+
},
7985
hir::ExprKind::MethodCall(method_name, receiver, [], ..) => {
8086
if (method_name.ident.name == sym::as_str || method_name.ident.name == sym::as_ref) && {
8187
let arg_type = cx.typeck_results().expr_ty(receiver);

tests/ui/expect_fun_call.fixed

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,3 +147,13 @@ fn main() {
147147
return;
148148
});
149149
}
150+
151+
fn issue16747() {
152+
let x = 42;
153+
let _c = char::from_u32(x).unwrap_or_else(|| panic!("{}", &format!("Illegal: {x}")[..]));
154+
//~^ expect_fun_call
155+
156+
let s = "hello";
157+
let _c = char::from_u32(x).unwrap_or_else(|| panic!("{}", &s.to_lowercase()[..2]));
158+
//~^ expect_fun_call
159+
}

tests/ui/expect_fun_call.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,3 +147,13 @@ fn main() {
147147
return;
148148
});
149149
}
150+
151+
fn issue16747() {
152+
let x = 42;
153+
let _c = char::from_u32(x).expect(&format!("Illegal: {x}")[..]);
154+
//~^ expect_fun_call
155+
156+
let s = "hello";
157+
let _c = char::from_u32(x).expect(&s.to_lowercase()[..2]);
158+
//~^ expect_fun_call
159+
}

tests/ui/expect_fun_call.stderr

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,5 +97,17 @@ error: function call inside of `expect`
9797
LL | format_capture_and_value.expect(&format!("{error_code}, {}", 1));
9898
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `unwrap_or_else(|| panic!("{error_code}, {}", 1))`
9999

100-
error: aborting due to 16 previous errors
100+
error: function call inside of `expect`
101+
--> tests/ui/expect_fun_call.rs:153:32
102+
|
103+
LL | let _c = char::from_u32(x).expect(&format!("Illegal: {x}")[..]);
104+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `unwrap_or_else(|| panic!("{}", &format!("Illegal: {x}")[..]))`
105+
106+
error: function call inside of `expect`
107+
--> tests/ui/expect_fun_call.rs:157:32
108+
|
109+
LL | let _c = char::from_u32(x).expect(&s.to_lowercase()[..2]);
110+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `unwrap_or_else(|| panic!("{}", &s.to_lowercase()[..2]))`
111+
112+
error: aborting due to 18 previous errors
101113

0 commit comments

Comments
 (0)