Skip to content

Commit e7c55a4

Browse files
committed
Fix #[expect] for clippy::ptr_arg
1 parent 93ebd0e commit e7c55a4

File tree

3 files changed

+52
-35
lines changed

3 files changed

+52
-35
lines changed

clippy_lints/src/ptr.rs

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! Checks for usage of `&Vec[_]` and `&String`.
22
3-
use clippy_utils::diagnostics::{span_lint, span_lint_and_sugg, span_lint_and_then};
3+
use clippy_utils::diagnostics::{span_lint, span_lint_and_sugg, span_lint_and_then, span_lint_hir_and_then};
44
use clippy_utils::source::snippet_opt;
55
use clippy_utils::ty::expr_sig;
66
use clippy_utils::visitors::contains_unsafe_block;
@@ -166,15 +166,14 @@ impl<'tcx> LateLintPass<'tcx> for Ptr {
166166
)
167167
.filter(|arg| arg.mutability() == Mutability::Not)
168168
{
169-
span_lint_and_sugg(
170-
cx,
171-
PTR_ARG,
172-
arg.span,
173-
&arg.build_msg(),
174-
"change this to",
175-
format!("{}{}", arg.ref_prefix, arg.deref_ty.display(cx)),
176-
Applicability::Unspecified,
177-
);
169+
span_lint_hir_and_then(cx, PTR_ARG, arg.emission_id, arg.span, &arg.build_msg(), |diag| {
170+
diag.span_suggestion(
171+
arg.span,
172+
"change this to",
173+
format!("{}{}", arg.ref_prefix, arg.deref_ty.display(cx)),
174+
Applicability::Unspecified,
175+
);
176+
});
178177
}
179178
}
180179
}
@@ -221,7 +220,7 @@ impl<'tcx> LateLintPass<'tcx> for Ptr {
221220
let results = check_ptr_arg_usage(cx, body, &lint_args);
222221

223222
for (result, args) in results.iter().zip(lint_args.iter()).filter(|(r, _)| !r.skip) {
224-
span_lint_and_then(cx, PTR_ARG, args.span, &args.build_msg(), |diag| {
223+
span_lint_hir_and_then(cx, PTR_ARG, args.emission_id, args.span, &args.build_msg(), |diag| {
225224
diag.multipart_suggestion(
226225
"change this to",
227226
iter::once((args.span, format!("{}{}", args.ref_prefix, args.deref_ty.display(cx))))
@@ -315,6 +314,7 @@ struct PtrArgReplacement {
315314

316315
struct PtrArg<'tcx> {
317316
idx: usize,
317+
emission_id: hir::HirId,
318318
span: Span,
319319
ty_did: DefId,
320320
ty_name: Symbol,
@@ -419,10 +419,8 @@ fn check_fn_args<'cx, 'tcx: 'cx>(
419419
if let [.., name] = path.segments;
420420
if cx.tcx.item_name(adt.did()) == name.ident.name;
421421

422-
if !is_lint_allowed(cx, PTR_ARG, hir_ty.hir_id);
423-
if params.get(i).map_or(true, |p| !is_lint_allowed(cx, PTR_ARG, p.hir_id));
424-
425422
then {
423+
let emission_id = params.get(i).map_or(hir_ty.hir_id, |param| param.hir_id);
426424
let (method_renames, deref_ty) = match cx.tcx.get_diagnostic_name(adt.did()) {
427425
Some(sym::Vec) => (
428426
[("clone", ".to_owned()")].as_slice(),
@@ -455,21 +453,28 @@ fn check_fn_args<'cx, 'tcx: 'cx>(
455453
})
456454
.and_then(|arg| snippet_opt(cx, arg.span))
457455
.unwrap_or_else(|| substs.type_at(1).to_string());
458-
span_lint_and_sugg(
456+
span_lint_hir_and_then(
459457
cx,
460458
PTR_ARG,
459+
emission_id,
461460
hir_ty.span,
462461
"using a reference to `Cow` is not recommended",
463-
"change this to",
464-
format!("&{}{}", mutability.prefix_str(), ty_name),
465-
Applicability::Unspecified,
462+
|diag| {
463+
diag.span_suggestion(
464+
hir_ty.span,
465+
"change this to",
466+
format!("&{}{}", mutability.prefix_str(), ty_name),
467+
Applicability::Unspecified,
468+
);
469+
}
466470
);
467471
return None;
468472
},
469473
_ => return None,
470474
};
471475
return Some(PtrArg {
472476
idx: i,
477+
emission_id: emission_id,
473478
span: hir_ty.span,
474479
ty_did: adt.did(),
475480
ty_name: name.ident.name,

tests/ui/ptr_arg.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#![feature(lint_reasons)]
12
#![allow(unused, clippy::many_single_char_names, clippy::redundant_clone)]
23
#![warn(clippy::ptr_arg)]
34

@@ -109,16 +110,20 @@ mod issue_5644 {
109110
#[allow(clippy::ptr_arg)] _s: &String,
110111
#[allow(clippy::ptr_arg)] _p: &PathBuf,
111112
#[allow(clippy::ptr_arg)] _c: &Cow<[i32]>,
113+
#[expect(clippy::ptr_arg)] _expect: &Cow<[i32]>,
112114
) {
113115
}
114116

117+
fn some_allowed(#[allow(clippy::ptr_arg)] _v: &Vec<u32>, _s: &String) {}
118+
115119
struct S;
116120
impl S {
117121
fn allowed(
118122
#[allow(clippy::ptr_arg)] _v: &Vec<u32>,
119123
#[allow(clippy::ptr_arg)] _s: &String,
120124
#[allow(clippy::ptr_arg)] _p: &PathBuf,
121125
#[allow(clippy::ptr_arg)] _c: &Cow<[i32]>,
126+
#[expect(clippy::ptr_arg)] _expect: &Cow<[i32]>,
122127
) {
123128
}
124129
}
@@ -129,6 +134,7 @@ mod issue_5644 {
129134
#[allow(clippy::ptr_arg)] _s: &String,
130135
#[allow(clippy::ptr_arg)] _p: &PathBuf,
131136
#[allow(clippy::ptr_arg)] _c: &Cow<[i32]>,
137+
#[expect(clippy::ptr_arg)] _expect: &Cow<[i32]>,
132138
) {
133139
}
134140
}

tests/ui/ptr_arg.stderr

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,49 @@
11
error: writing `&Vec` instead of `&[_]` involves a new object where a slice will do
2-
--> $DIR/ptr_arg.rs:7:14
2+
--> $DIR/ptr_arg.rs:8:14
33
|
44
LL | fn do_vec(x: &Vec<i64>) {
55
| ^^^^^^^^^ help: change this to: `&[i64]`
66
|
77
= note: `-D clippy::ptr-arg` implied by `-D warnings`
88

99
error: writing `&mut Vec` instead of `&mut [_]` involves a new object where a slice will do
10-
--> $DIR/ptr_arg.rs:11:18
10+
--> $DIR/ptr_arg.rs:12:18
1111
|
1212
LL | fn do_vec_mut(x: &mut Vec<i64>) {
1313
| ^^^^^^^^^^^^^ help: change this to: `&mut [i64]`
1414

1515
error: writing `&String` instead of `&str` involves a new object where a slice will do
16-
--> $DIR/ptr_arg.rs:15:14
16+
--> $DIR/ptr_arg.rs:16:14
1717
|
1818
LL | fn do_str(x: &String) {
1919
| ^^^^^^^ help: change this to: `&str`
2020

2121
error: writing `&mut String` instead of `&mut str` involves a new object where a slice will do
22-
--> $DIR/ptr_arg.rs:19:18
22+
--> $DIR/ptr_arg.rs:20:18
2323
|
2424
LL | fn do_str_mut(x: &mut String) {
2525
| ^^^^^^^^^^^ help: change this to: `&mut str`
2626

2727
error: writing `&PathBuf` instead of `&Path` involves a new object where a slice will do
28-
--> $DIR/ptr_arg.rs:23:15
28+
--> $DIR/ptr_arg.rs:24:15
2929
|
3030
LL | fn do_path(x: &PathBuf) {
3131
| ^^^^^^^^ help: change this to: `&Path`
3232

3333
error: writing `&mut PathBuf` instead of `&mut Path` involves a new object where a slice will do
34-
--> $DIR/ptr_arg.rs:27:19
34+
--> $DIR/ptr_arg.rs:28:19
3535
|
3636
LL | fn do_path_mut(x: &mut PathBuf) {
3737
| ^^^^^^^^^^^^ help: change this to: `&mut Path`
3838

3939
error: writing `&Vec` instead of `&[_]` involves a new object where a slice will do
40-
--> $DIR/ptr_arg.rs:35:18
40+
--> $DIR/ptr_arg.rs:36:18
4141
|
4242
LL | fn do_vec(x: &Vec<i64>);
4343
| ^^^^^^^^^ help: change this to: `&[i64]`
4444

4545
error: writing `&Vec` instead of `&[_]` involves a new object where a slice will do
46-
--> $DIR/ptr_arg.rs:48:14
46+
--> $DIR/ptr_arg.rs:49:14
4747
|
4848
LL | fn cloned(x: &Vec<u8>) -> Vec<u8> {
4949
| ^^^^^^^^
@@ -59,7 +59,7 @@ LL | let i = (e).clone();
5959
...
6060

6161
error: writing `&String` instead of `&str` involves a new object where a slice will do
62-
--> $DIR/ptr_arg.rs:57:18
62+
--> $DIR/ptr_arg.rs:58:18
6363
|
6464
LL | fn str_cloned(x: &String) -> String {
6565
| ^^^^^^^
@@ -75,7 +75,7 @@ LL ~ x.to_owned()
7575
|
7676

7777
error: writing `&PathBuf` instead of `&Path` involves a new object where a slice will do
78-
--> $DIR/ptr_arg.rs:65:19
78+
--> $DIR/ptr_arg.rs:66:19
7979
|
8080
LL | fn path_cloned(x: &PathBuf) -> PathBuf {
8181
| ^^^^^^^^
@@ -91,7 +91,7 @@ LL ~ x.to_path_buf()
9191
|
9292

9393
error: writing `&String` instead of `&str` involves a new object where a slice will do
94-
--> $DIR/ptr_arg.rs:73:44
94+
--> $DIR/ptr_arg.rs:74:44
9595
|
9696
LL | fn false_positive_capacity(x: &Vec<u8>, y: &String) {
9797
| ^^^^^^^
@@ -105,13 +105,19 @@ LL ~ let c = y;
105105
|
106106

107107
error: using a reference to `Cow` is not recommended
108-
--> $DIR/ptr_arg.rs:87:25
108+
--> $DIR/ptr_arg.rs:88:25
109109
|
110110
LL | fn test_cow_with_ref(c: &Cow<[i32]>) {}
111111
| ^^^^^^^^^^^ help: change this to: `&[i32]`
112112

113+
error: writing `&String` instead of `&str` involves a new object where a slice will do
114+
--> $DIR/ptr_arg.rs:117:66
115+
|
116+
LL | fn some_allowed(#[allow(clippy::ptr_arg)] _v: &Vec<u32>, _s: &String) {}
117+
| ^^^^^^^ help: change this to: `&str`
118+
113119
error: writing `&Vec` instead of `&[_]` involves a new object where a slice will do
114-
--> $DIR/ptr_arg.rs:140:21
120+
--> $DIR/ptr_arg.rs:146:21
115121
|
116122
LL | fn foo_vec(vec: &Vec<u8>) {
117123
| ^^^^^^^^
@@ -124,7 +130,7 @@ LL ~ let _ = vec.to_owned().clone();
124130
|
125131

126132
error: writing `&PathBuf` instead of `&Path` involves a new object where a slice will do
127-
--> $DIR/ptr_arg.rs:145:23
133+
--> $DIR/ptr_arg.rs:151:23
128134
|
129135
LL | fn foo_path(path: &PathBuf) {
130136
| ^^^^^^^^
@@ -137,7 +143,7 @@ LL ~ let _ = path.to_path_buf().clone();
137143
|
138144

139145
error: writing `&PathBuf` instead of `&Path` involves a new object where a slice will do
140-
--> $DIR/ptr_arg.rs:150:21
146+
--> $DIR/ptr_arg.rs:156:21
141147
|
142148
LL | fn foo_str(str: &PathBuf) {
143149
| ^^^^^^^^
@@ -150,10 +156,10 @@ LL ~ let _ = str.to_path_buf().clone();
150156
|
151157

152158
error: writing `&mut Vec` instead of `&mut [_]` involves a new object where a slice will do
153-
--> $DIR/ptr_arg.rs:156:29
159+
--> $DIR/ptr_arg.rs:162:29
154160
|
155161
LL | fn mut_vec_slice_methods(v: &mut Vec<u32>) {
156162
| ^^^^^^^^^^^^^ help: change this to: `&mut [u32]`
157163

158-
error: aborting due to 16 previous errors
164+
error: aborting due to 17 previous errors
159165

0 commit comments

Comments
 (0)