Skip to content

Commit d7056f8

Browse files
committed
Refine lint message.
1 parent 4f14826 commit d7056f8

File tree

2 files changed

+20
-20
lines changed

2 files changed

+20
-20
lines changed

clippy_lints/src/methods/mod.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -3222,17 +3222,17 @@ fn lint_option_as_ref_deref<'a, 'tcx>(
32223222

32233223
if is_deref {
32243224
let current_method = if is_mut {
3225-
".as_mut().map(DerefMut::deref_mut)"
3225+
format!(".as_mut().map({})", snippet(cx, map_args[1].span, ".."))
32263226
} else {
3227-
".as_ref().map(Deref::deref)"
3227+
format!(".as_ref().map({})", snippet(cx, map_args[1].span, ".."))
32283228
};
32293229
let method_hint = if is_mut { "as_deref_mut" } else { "as_deref" };
32303230
let hint = format!("{}.{}()", snippet(cx, as_ref_args[0].span, ".."), method_hint);
32313231
let suggestion = format!("try using {} instead", method_hint);
32323232

32333233
let msg = format!(
3234-
"called `{0}` (or with one of deref aliases) on an Option value. \
3235-
This can be done more directly by calling `{1}` instead",
3234+
"called `{0}` on an Option value. This can be done more directly \
3235+
by calling `{1}` instead",
32363236
current_method, hint
32373237
);
32383238
span_lint_and_sugg(

tests/ui/option_as_ref_deref.stderr

+16-16
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
error: called `.as_ref().map(Deref::deref)` (or with one of deref aliases) on an Option value. This can be done more directly by calling `opt.clone().as_deref()` instead
1+
error: called `.as_ref().map(Deref::deref)` on an Option value. This can be done more directly by calling `opt.clone().as_deref()` instead
22
--> $DIR/option_as_ref_deref.rs:13:13
33
|
44
LL | let _ = opt.clone().as_ref().map(Deref::deref).map(str::len);
55
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try using as_deref instead: `opt.clone().as_deref()`
66
|
77
= note: `-D clippy::option-as-ref-deref` implied by `-D warnings`
88

9-
error: called `.as_ref().map(Deref::deref)` (or with one of deref aliases) on an Option value. This can be done more directly by calling `opt.clone().as_deref()` instead
9+
error: called `.as_ref().map(Deref::deref)` on an Option value. This can be done more directly by calling `opt.clone().as_deref()` instead
1010
--> $DIR/option_as_ref_deref.rs:16:13
1111
|
1212
LL | let _ = opt.clone()
@@ -16,85 +16,85 @@ LL | | Deref::deref
1616
LL | | )
1717
| |_________^ help: try using as_deref instead: `opt.clone().as_deref()`
1818

19-
error: called `.as_mut().map(DerefMut::deref_mut)` (or with one of deref aliases) on an Option value. This can be done more directly by calling `opt.as_deref_mut()` instead
19+
error: called `.as_mut().map(DerefMut::deref_mut)` on an Option value. This can be done more directly by calling `opt.as_deref_mut()` instead
2020
--> $DIR/option_as_ref_deref.rs:22:13
2121
|
2222
LL | let _ = opt.as_mut().map(DerefMut::deref_mut);
2323
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try using as_deref_mut instead: `opt.as_deref_mut()`
2424

25-
error: called `.as_ref().map(Deref::deref)` (or with one of deref aliases) on an Option value. This can be done more directly by calling `opt.as_deref()` instead
25+
error: called `.as_ref().map(String::as_str)` on an Option value. This can be done more directly by calling `opt.as_deref()` instead
2626
--> $DIR/option_as_ref_deref.rs:24:13
2727
|
2828
LL | let _ = opt.as_ref().map(String::as_str);
2929
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try using as_deref instead: `opt.as_deref()`
3030

31-
error: called `.as_ref().map(Deref::deref)` (or with one of deref aliases) on an Option value. This can be done more directly by calling `opt.as_deref()` instead
31+
error: called `.as_ref().map(|x| x.as_str())` on an Option value. This can be done more directly by calling `opt.as_deref()` instead
3232
--> $DIR/option_as_ref_deref.rs:25:13
3333
|
3434
LL | let _ = opt.as_ref().map(|x| x.as_str());
3535
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try using as_deref instead: `opt.as_deref()`
3636

37-
error: called `.as_mut().map(DerefMut::deref_mut)` (or with one of deref aliases) on an Option value. This can be done more directly by calling `opt.as_deref_mut()` instead
37+
error: called `.as_mut().map(String::as_mut_str)` on an Option value. This can be done more directly by calling `opt.as_deref_mut()` instead
3838
--> $DIR/option_as_ref_deref.rs:26:13
3939
|
4040
LL | let _ = opt.as_mut().map(String::as_mut_str);
4141
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try using as_deref_mut instead: `opt.as_deref_mut()`
4242

43-
error: called `.as_mut().map(DerefMut::deref_mut)` (or with one of deref aliases) on an Option value. This can be done more directly by calling `opt.as_deref_mut()` instead
43+
error: called `.as_mut().map(|x| x.as_mut_str())` on an Option value. This can be done more directly by calling `opt.as_deref_mut()` instead
4444
--> $DIR/option_as_ref_deref.rs:27:13
4545
|
4646
LL | let _ = opt.as_mut().map(|x| x.as_mut_str());
4747
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try using as_deref_mut instead: `opt.as_deref_mut()`
4848

49-
error: called `.as_ref().map(Deref::deref)` (or with one of deref aliases) on an Option value. This can be done more directly by calling `Some(CString::new(vec![]).unwrap()).as_deref()` instead
49+
error: called `.as_ref().map(CString::as_c_str)` on an Option value. This can be done more directly by calling `Some(CString::new(vec![]).unwrap()).as_deref()` instead
5050
--> $DIR/option_as_ref_deref.rs:28:13
5151
|
5252
LL | let _ = Some(CString::new(vec![]).unwrap()).as_ref().map(CString::as_c_str);
5353
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try using as_deref instead: `Some(CString::new(vec![]).unwrap()).as_deref()`
5454

55-
error: called `.as_ref().map(Deref::deref)` (or with one of deref aliases) on an Option value. This can be done more directly by calling `Some(OsString::new()).as_deref()` instead
55+
error: called `.as_ref().map(OsString::as_os_str)` on an Option value. This can be done more directly by calling `Some(OsString::new()).as_deref()` instead
5656
--> $DIR/option_as_ref_deref.rs:29:13
5757
|
5858
LL | let _ = Some(OsString::new()).as_ref().map(OsString::as_os_str);
5959
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try using as_deref instead: `Some(OsString::new()).as_deref()`
6060

61-
error: called `.as_ref().map(Deref::deref)` (or with one of deref aliases) on an Option value. This can be done more directly by calling `Some(PathBuf::new()).as_deref()` instead
61+
error: called `.as_ref().map(PathBuf::as_path)` on an Option value. This can be done more directly by calling `Some(PathBuf::new()).as_deref()` instead
6262
--> $DIR/option_as_ref_deref.rs:30:13
6363
|
6464
LL | let _ = Some(PathBuf::new()).as_ref().map(PathBuf::as_path);
6565
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try using as_deref instead: `Some(PathBuf::new()).as_deref()`
6666

67-
error: called `.as_ref().map(Deref::deref)` (or with one of deref aliases) on an Option value. This can be done more directly by calling `Some(Vec::<()>::new()).as_deref()` instead
67+
error: called `.as_ref().map(Vec::as_slice)` on an Option value. This can be done more directly by calling `Some(Vec::<()>::new()).as_deref()` instead
6868
--> $DIR/option_as_ref_deref.rs:31:13
6969
|
7070
LL | let _ = Some(Vec::<()>::new()).as_ref().map(Vec::as_slice);
7171
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try using as_deref instead: `Some(Vec::<()>::new()).as_deref()`
7272

73-
error: called `.as_mut().map(DerefMut::deref_mut)` (or with one of deref aliases) on an Option value. This can be done more directly by calling `Some(Vec::<()>::new()).as_deref_mut()` instead
73+
error: called `.as_mut().map(Vec::as_mut_slice)` on an Option value. This can be done more directly by calling `Some(Vec::<()>::new()).as_deref_mut()` instead
7474
--> $DIR/option_as_ref_deref.rs:32:13
7575
|
7676
LL | let _ = Some(Vec::<()>::new()).as_mut().map(Vec::as_mut_slice);
7777
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try using as_deref_mut instead: `Some(Vec::<()>::new()).as_deref_mut()`
7878

79-
error: called `.as_ref().map(Deref::deref)` (or with one of deref aliases) on an Option value. This can be done more directly by calling `opt.as_deref()` instead
79+
error: called `.as_ref().map(|x| x.deref())` on an Option value. This can be done more directly by calling `opt.as_deref()` instead
8080
--> $DIR/option_as_ref_deref.rs:34:13
8181
|
8282
LL | let _ = opt.as_ref().map(|x| x.deref());
8383
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try using as_deref instead: `opt.as_deref()`
8484

85-
error: called `.as_mut().map(DerefMut::deref_mut)` (or with one of deref aliases) on an Option value. This can be done more directly by calling `opt.clone().as_deref_mut()` instead
85+
error: called `.as_mut().map(|x| x.deref_mut())` on an Option value. This can be done more directly by calling `opt.clone().as_deref_mut()` instead
8686
--> $DIR/option_as_ref_deref.rs:35:13
8787
|
8888
LL | let _ = opt.clone().as_mut().map(|x| x.deref_mut()).map(|x| x.len());
8989
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try using as_deref_mut instead: `opt.clone().as_deref_mut()`
9090

91-
error: called `.as_ref().map(Deref::deref)` (or with one of deref aliases) on an Option value. This can be done more directly by calling `opt.as_deref()` instead
91+
error: called `.as_ref().map(|x| &**x)` on an Option value. This can be done more directly by calling `opt.as_deref()` instead
9292
--> $DIR/option_as_ref_deref.rs:42:13
9393
|
9494
LL | let _ = opt.as_ref().map(|x| &**x);
9595
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try using as_deref instead: `opt.as_deref()`
9696

97-
error: called `.as_mut().map(DerefMut::deref_mut)` (or with one of deref aliases) on an Option value. This can be done more directly by calling `opt.as_deref_mut()` instead
97+
error: called `.as_mut().map(|x| &mut **x)` on an Option value. This can be done more directly by calling `opt.as_deref_mut()` instead
9898
--> $DIR/option_as_ref_deref.rs:43:13
9999
|
100100
LL | let _ = opt.as_mut().map(|x| &mut **x);

0 commit comments

Comments
 (0)