Skip to content

Commit 1d58fe2

Browse files
committed
fix test errors
1 parent 24c3ac1 commit 1d58fe2

6 files changed

+190
-56
lines changed

clippy_lints/src/path_from_format.rs

+10-9
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use rustc_hir::{Expr, ExprKind};
66
use rustc_lint::{LateContext, LateLintPass};
77
use rustc_session::{declare_lint_pass, declare_tool_lint};
88
use rustc_span::sym;
9+
use std::fmt::Write as _;
910

1011
declare_clippy_lint! {
1112
/// ### What it does
@@ -33,18 +34,18 @@ declare_lint_pass!(PathFromFormat => [PATH_FROM_FORMAT]);
3334
impl<'tcx> LateLintPass<'tcx> for PathFromFormat {
3435
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
3536
if_chain! {
36-
if let ExprKind::Call(_, ref args) = expr.kind;
37+
if let ExprKind::Call(_, args) = expr.kind;
3738
if let ty = cx.typeck_results().expr_ty(expr);
3839
if is_type_diagnostic_item(cx, ty, sym::PathBuf);
39-
if args.len() > 0;
40+
if !args.is_empty();
4041
if let Some(macro_def_id) = args[0].span.ctxt().outer_expn_data().macro_def_id;
4142
if cx.tcx.get_diagnostic_name(macro_def_id) == Some(sym::format_macro);
4243
then {
4344
let full_expr = snippet(cx, expr.span, "error").to_string();
4445
let split_expr: Vec<&str> = full_expr.split('!').collect();
4546
let args_to_macro = split_expr[1];
4647
let replaced = args_to_macro.replace('(', "").replace(')', "");
47-
let unformatted: Vec<&str> = replaced.split(",").collect();
48+
let unformatted: Vec<&str> = replaced.split(',').collect();
4849
let mut push_targets: Vec<String> = Vec::new();
4950
let mut temp_string = String::new();
5051
for c in unformatted[0].chars() {
@@ -65,20 +66,20 @@ impl<'tcx> LateLintPass<'tcx> for PathFromFormat {
6566
}
6667
for target in push_targets {
6768
let target_processed =
68-
if target != unformatted[1].replace(' ', "") {
69+
if target == unformatted[1].replace(' ', "") {
70+
target
71+
}
72+
else {
6973
let mut s = String::from("\"");
7074
s.push_str(&target);
7175
s.push('"');
7276
s
73-
}
74-
else {
75-
target
7677
};
7778
if temp_string.is_empty() {
78-
temp_string.push_str(&format!("Path::new({})", target_processed));
79+
let _ = write!(temp_string, "Path::new({})", target_processed);
7980
}
8081
else {
81-
temp_string.push_str(&format!(".join({})", target_processed));
82+
let _ = write!(temp_string, ".join({})", target_processed);
8283
}
8384
}
8485
span_lint_and_sugg(

tests/ui/eta.stderr

+44-10
Original file line numberDiff line numberDiff line change
@@ -68,19 +68,53 @@ error: redundant closure
6868
LL | let e: std::vec::Vec<char> = vec!['a', 'b', 'c'].iter().map(|c| c.to_ascii_uppercase()).collect();
6969
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace the closure with the method itself: `char::to_ascii_uppercase`
7070

71-
thread 'rustc' panicked at 'index out of bounds: the len is 0 but the index is 0', clippy_lints/src/path_from_format.rs:39:41
72-
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
71+
error: redundant closure
72+
--> $DIR/eta.rs:160:22
73+
|
74+
LL | requires_fn_once(|| x());
75+
| ^^^^^^ help: replace the closure with the function itself: `x`
7376

74-
error: internal compiler error: unexpected panic
77+
error: redundant closure
78+
--> $DIR/eta.rs:167:27
79+
|
80+
LL | let a = Some(1u8).map(|a| foo_ptr(a));
81+
| ^^^^^^^^^^^^^^ help: replace the closure with the function itself: `foo_ptr`
7582

76-
note: the compiler unexpectedly panicked. this is a bug.
83+
error: redundant closure
84+
--> $DIR/eta.rs:172:27
85+
|
86+
LL | let a = Some(1u8).map(|a| closure(a));
87+
| ^^^^^^^^^^^^^^ help: replace the closure with the function itself: `closure`
7788

78-
note: we would appreciate a bug report: https://github.com/rust-lang/rust-clippy/issues/new
89+
error: redundant closure
90+
--> $DIR/eta.rs:204:28
91+
|
92+
LL | x.into_iter().for_each(|x| add_to_res(x));
93+
| ^^^^^^^^^^^^^^^^^ help: replace the closure with the function itself: `&mut add_to_res`
7994

80-
note: Clippy version: clippy 0.1.62 (01d75b195 2022-05-13)
95+
error: redundant closure
96+
--> $DIR/eta.rs:205:28
97+
|
98+
LL | y.into_iter().for_each(|x| add_to_res(x));
99+
| ^^^^^^^^^^^^^^^^^ help: replace the closure with the function itself: `&mut add_to_res`
100+
101+
error: redundant closure
102+
--> $DIR/eta.rs:206:28
103+
|
104+
LL | z.into_iter().for_each(|x| add_to_res(x));
105+
| ^^^^^^^^^^^^^^^^^ help: replace the closure with the function itself: `add_to_res`
106+
107+
error: redundant closure
108+
--> $DIR/eta.rs:213:21
109+
|
110+
LL | Some(1).map(|n| closure(n));
111+
| ^^^^^^^^^^^^^^ help: replace the closure with the function itself: `&mut closure`
112+
113+
error: redundant closure
114+
--> $DIR/eta.rs:217:21
115+
|
116+
LL | Some(1).map(|n| in_loop(n));
117+
| ^^^^^^^^^^^^^^ help: replace the closure with the function itself: `in_loop`
81118

82-
query stack during panic:
83-
#0 [analysis] running analysis passes on this crate
84-
end of query stack
85-
error: aborting due to 11 previous errors
119+
error: aborting due to 19 previous errors
86120

tests/ui/implicit_clone.stderr

+6-6
Original file line numberDiff line numberDiff line change
@@ -37,37 +37,37 @@ LL | let _ = kitten.to_owned();
3737
| ^^^^^^^^^^^^^^^^^ help: consider using: `kitten.clone()`
3838

3939
error: implicitly cloning a `PathBuf` by calling `to_owned` on its dereferenced type
40-
--> $DIR/implicit_clone.rs:98:13
40+
--> $DIR/implicit_clone.rs:97:13
4141
|
4242
LL | let _ = pathbuf.to_owned();
4343
| ^^^^^^^^^^^^^^^^^^ help: consider using: `pathbuf.clone()`
4444

4545
error: implicitly cloning a `PathBuf` by calling `to_path_buf` on its dereferenced type
46-
--> $DIR/implicit_clone.rs:99:13
46+
--> $DIR/implicit_clone.rs:98:13
4747
|
4848
LL | let _ = pathbuf.to_path_buf();
4949
| ^^^^^^^^^^^^^^^^^^^^^ help: consider using: `pathbuf.clone()`
5050

5151
error: implicitly cloning a `OsString` by calling `to_owned` on its dereferenced type
52-
--> $DIR/implicit_clone.rs:102:13
52+
--> $DIR/implicit_clone.rs:101:13
5353
|
5454
LL | let _ = os_string.to_owned();
5555
| ^^^^^^^^^^^^^^^^^^^^ help: consider using: `os_string.clone()`
5656

5757
error: implicitly cloning a `OsString` by calling `to_os_string` on its dereferenced type
58-
--> $DIR/implicit_clone.rs:103:13
58+
--> $DIR/implicit_clone.rs:102:13
5959
|
6060
LL | let _ = os_string.to_os_string();
6161
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `os_string.clone()`
6262

6363
error: implicitly cloning a `PathBuf` by calling `to_path_buf` on its dereferenced type
64-
--> $DIR/implicit_clone.rs:114:13
64+
--> $DIR/implicit_clone.rs:113:13
6565
|
6666
LL | let _ = pathbuf_ref.to_path_buf();
6767
| ^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `(*pathbuf_ref).clone()`
6868

6969
error: implicitly cloning a `PathBuf` by calling `to_path_buf` on its dereferenced type
70-
--> $DIR/implicit_clone.rs:117:13
70+
--> $DIR/implicit_clone.rs:116:13
7171
|
7272
LL | let _ = pathbuf_ref.to_path_buf();
7373
| ^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using: `(**pathbuf_ref).clone()`

tests/ui/option_as_ref_deref.stderr

+38-10
Original file line numberDiff line numberDiff line change
@@ -64,19 +64,47 @@ error: called `.as_ref().map(PathBuf::as_path)` on an Option value. This can be
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-
thread 'rustc' panicked at 'index out of bounds: the len is 0 but the index is 0', clippy_lints/src/path_from_format.rs:39:41
68-
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
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
68+
--> $DIR/option_as_ref_deref.rs:31:13
69+
|
70+
LL | let _ = Some(Vec::<()>::new()).as_ref().map(Vec::as_slice);
71+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try using as_deref instead: `Some(Vec::<()>::new()).as_deref()`
6972

70-
error: internal compiler error: unexpected panic
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
74+
--> $DIR/option_as_ref_deref.rs:32:13
75+
|
76+
LL | let _ = Some(Vec::<()>::new()).as_mut().map(Vec::as_mut_slice);
77+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try using as_deref_mut instead: `Some(Vec::<()>::new()).as_deref_mut()`
7178

72-
note: the compiler unexpectedly panicked. this is a bug.
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
80+
--> $DIR/option_as_ref_deref.rs:34:13
81+
|
82+
LL | let _ = opt.as_ref().map(|x| x.deref());
83+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try using as_deref instead: `opt.as_deref()`
7384

74-
note: we would appreciate a bug report: https://github.com/rust-lang/rust-clippy/issues/new
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
86+
--> $DIR/option_as_ref_deref.rs:35:13
87+
|
88+
LL | let _ = opt.clone().as_mut().map(|x| x.deref_mut()).map(|x| x.len());
89+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try using as_deref_mut instead: `opt.clone().as_deref_mut()`
7590

76-
note: Clippy version: clippy 0.1.62 (01d75b195 2022-05-13)
91+
error: called `.as_ref().map(|x| &**x)` on an Option value. This can be done more directly by calling `opt.as_deref()` instead
92+
--> $DIR/option_as_ref_deref.rs:42:13
93+
|
94+
LL | let _ = opt.as_ref().map(|x| &**x);
95+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try using as_deref instead: `opt.as_deref()`
96+
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
98+
--> $DIR/option_as_ref_deref.rs:43:13
99+
|
100+
LL | let _ = opt.as_mut().map(|x| &mut **x);
101+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try using as_deref_mut instead: `opt.as_deref_mut()`
102+
103+
error: called `.as_ref().map(std::ops::Deref::deref)` on an Option value. This can be done more directly by calling `opt.as_deref()` instead
104+
--> $DIR/option_as_ref_deref.rs:46:13
105+
|
106+
LL | let _ = opt.as_ref().map(std::ops::Deref::deref);
107+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try using as_deref instead: `opt.as_deref()`
77108

78-
query stack during panic:
79-
#0 [analysis] running analysis passes on this crate
80-
end of query stack
81-
error: aborting due to 10 previous errors
109+
error: aborting due to 17 previous errors
82110

tests/ui/unnecessary_iter_cloned.stderr

+32-11
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,35 @@
1-
thread 'rustc' panicked at 'index out of bounds: the len is 0 but the index is 0', clippy_lints/src/path_from_format.rs:39:41
2-
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
1+
error: unnecessary use of `copied`
2+
--> $DIR/unnecessary_iter_cloned.rs:31:22
3+
|
4+
LL | for (t, path) in files.iter().copied() {
5+
| ^^^^^^^^^^^^^^^^^^^^^
6+
|
7+
= note: `-D clippy::unnecessary-to-owned` implied by `-D warnings`
8+
help: use
9+
|
10+
LL | for (t, path) in files {
11+
| ~~~~~
12+
help: remove this `&`
13+
|
14+
LL - let other = match get_file_path(&t) {
15+
LL + let other = match get_file_path(t) {
16+
|
317

4-
error: internal compiler error: unexpected panic
18+
error: unnecessary use of `copied`
19+
--> $DIR/unnecessary_iter_cloned.rs:46:22
20+
|
21+
LL | for (t, path) in files.iter().copied() {
22+
| ^^^^^^^^^^^^^^^^^^^^^
23+
|
24+
help: use
25+
|
26+
LL | for (t, path) in files.iter() {
27+
| ~~~~~~~~~~~~
28+
help: remove this `&`
29+
|
30+
LL - let other = match get_file_path(&t) {
31+
LL + let other = match get_file_path(t) {
32+
|
533

6-
note: the compiler unexpectedly panicked. this is a bug.
34+
error: aborting due to 2 previous errors
735

8-
note: we would appreciate a bug report: https://github.com/rust-lang/rust-clippy/issues/new
9-
10-
note: Clippy version: clippy 0.1.62 (01d75b195 2022-05-13)
11-
12-
query stack during panic:
13-
#0 [analysis] running analysis passes on this crate
14-
end of query stack

tests/ui/unnecessary_to_owned.stderr

+60-10
Original file line numberDiff line numberDiff line change
@@ -445,19 +445,69 @@ error: unnecessary use of `to_vec`
445445
LL | let _ = [std::path::PathBuf::new()][..].to_vec().into_iter();
446446
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use: `[std::path::PathBuf::new()][..].iter().cloned()`
447447

448-
thread 'rustc' panicked at 'index out of bounds: the len is 0 but the index is 0', clippy_lints/src/path_from_format.rs:39:41
449-
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
448+
error: unnecessary use of `to_owned`
449+
--> $DIR/unnecessary_to_owned.rs:131:13
450+
|
451+
LL | let _ = [std::path::PathBuf::new()][..].to_owned().into_iter();
452+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use: `[std::path::PathBuf::new()][..].iter().cloned()`
450453

451-
error: internal compiler error: unexpected panic
454+
error: unnecessary use of `to_vec`
455+
--> $DIR/unnecessary_to_owned.rs:133:13
456+
|
457+
LL | let _ = IntoIterator::into_iter(slice.to_vec());
458+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use: `slice.iter().copied()`
452459

453-
note: the compiler unexpectedly panicked. this is a bug.
460+
error: unnecessary use of `to_owned`
461+
--> $DIR/unnecessary_to_owned.rs:134:13
462+
|
463+
LL | let _ = IntoIterator::into_iter(slice.to_owned());
464+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use: `slice.iter().copied()`
454465

455-
note: we would appreciate a bug report: https://github.com/rust-lang/rust-clippy/issues/new
466+
error: unnecessary use of `to_vec`
467+
--> $DIR/unnecessary_to_owned.rs:135:13
468+
|
469+
LL | let _ = IntoIterator::into_iter([std::path::PathBuf::new()][..].to_vec());
470+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use: `[std::path::PathBuf::new()][..].iter().cloned()`
471+
472+
error: unnecessary use of `to_owned`
473+
--> $DIR/unnecessary_to_owned.rs:136:13
474+
|
475+
LL | let _ = IntoIterator::into_iter([std::path::PathBuf::new()][..].to_owned());
476+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use: `[std::path::PathBuf::new()][..].iter().cloned()`
456477

457-
note: Clippy version: clippy 0.1.62 (01d75b195 2022-05-13)
478+
error: unnecessary use of `to_vec`
479+
--> $DIR/unnecessary_to_owned.rs:198:14
480+
|
481+
LL | for t in file_types.to_vec() {
482+
| ^^^^^^^^^^^^^^^^^^^
483+
|
484+
help: use
485+
|
486+
LL | for t in file_types {
487+
| ~~~~~~~~~~
488+
help: remove this `&`
489+
|
490+
LL - let path = match get_file_path(&t) {
491+
LL + let path = match get_file_path(t) {
492+
|
493+
494+
error: unnecessary use of `to_vec`
495+
--> $DIR/unnecessary_to_owned.rs:221:14
496+
|
497+
LL | let _ = &["x"][..].to_vec().into_iter();
498+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use: `["x"][..].iter().cloned()`
499+
500+
error: unnecessary use of `to_vec`
501+
--> $DIR/unnecessary_to_owned.rs:226:14
502+
|
503+
LL | let _ = &["x"][..].to_vec().into_iter();
504+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: use: `["x"][..].iter().copied()`
505+
506+
error: unnecessary use of `to_string`
507+
--> $DIR/unnecessary_to_owned.rs:273:24
508+
|
509+
LL | Box::new(build(y.to_string()))
510+
| ^^^^^^^^^^^^^ help: use: `y`
458511

459-
query stack during panic:
460-
#0 [analysis] running analysis passes on this crate
461-
end of query stack
462-
error: aborting due to 69 previous errors
512+
error: aborting due to 78 previous errors
463513

0 commit comments

Comments
 (0)