Skip to content

Commit 202a80c

Browse files
RickyRicky
authored andcommitted
Added tests for map_err, ignored map_err lint on drop_ref tests
1 parent e49a299 commit 202a80c

File tree

5 files changed

+82
-38
lines changed

5 files changed

+82
-38
lines changed

clippy_lints/src/map_err_ignore.rs

Lines changed: 29 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::utils::span_lint_and_sugg;
22
use rustc_errors::Applicability;
3-
use rustc_hir::{Expr, ExprKind, CaptureBy, PatKind, QPath};
3+
use rustc_hir::{CaptureBy, Expr, ExprKind, PatKind, QPath};
44
use rustc_lint::{LateContext, LateLintPass};
55
use rustc_session::{declare_lint_pass, declare_tool_lint};
66

@@ -18,7 +18,7 @@ declare_clippy_lint! {
1818
/// Ignore
1919
///}
2020
///fn main() -> Result<(), Errors> {
21-
///
21+
///
2222
/// let x = u32::try_from(-123_i32);
2323
///
2424
/// println!("{:?}", x.map_err(|_| Errors::Ignore));
@@ -32,7 +32,7 @@ declare_clippy_lint! {
3232
/// WithContext(TryFromIntError)
3333
///}
3434
///fn main() -> Result<(), Errors> {
35-
///
35+
///
3636
/// let x = u32::try_from(-123_i32);
3737
///
3838
/// println!("{:?}", x.map_err(|e| Errors::WithContext(e)));
@@ -48,34 +48,42 @@ declare_clippy_lint! {
4848
declare_lint_pass!(MapErrIgnore => [MAP_ERR_IGNORE]);
4949

5050
impl<'tcx> LateLintPass<'tcx> for MapErrIgnore {
51-
// do not try to lint if this is from a macro or desugaring
51+
// do not try to lint if this is from a macro or desugaring
5252
fn check_expr(&mut self, cx: &LateContext<'_>, e: &Expr<'_>) {
5353
if e.span.from_expansion() {
5454
return;
5555
}
5656

5757
// check if this is a method call (e.g. x.foo())
5858
if let ExprKind::MethodCall(ref method, _t_span, ref args, _) = e.kind {
59-
// only work if the method name is `map_err` and there are only 2 arguments (e.g. x.map_err(|_|[1] Enum::Variant[2]))
59+
// only work if the method name is `map_err` and there are only 2 arguments (e.g. x.map_err(|_|[1]
60+
// Enum::Variant[2]))
6061
if method.ident.as_str() == "map_err" && args.len() == 2 {
6162
// make sure the first argument is a closure, and grab the CaptureRef, body_id, and body_span fields
62-
if let ExprKind::Closure(capture, _, body_id, body_span, _) = args[1].kind {
63+
if let ExprKind::Closure(capture, _, body_id, body_span, _) = args[1].kind {
6364
// check if this is by Reference (meaning there's no move statement)
64-
if capture == CaptureBy::Ref {
65-
// Get the closure body to check the parameters and values
65+
if capture == CaptureBy::Ref {
66+
// Get the closure body to check the parameters and values
6667
let closure_body = cx.tcx.hir().body(body_id);
6768
// make sure there's only one parameter (`|_|`)
68-
if closure_body.params.len() == 1 {
69-
// make sure that parameter is the wild token (`_`)
69+
if closure_body.params.len() == 1 {
70+
// make sure that parameter is the wild token (`_`)
7071
if let PatKind::Wild = closure_body.params[0].pat.kind {
71-
// Check the value of the closure to see if we can build the enum we are throwing away the error for
72-
// make sure this is a Path
72+
// Check the value of the closure to see if we can build the enum we are throwing away
73+
// the error for make sure this is a Path
7374
if let ExprKind::Path(q_path) = &closure_body.value.kind {
7475
// this should be a resolved path, only keep the path field
7576
if let QPath::Resolved(_, path) = q_path {
76-
// finally get the idents for each path segment collect them as a string and join them with the path separator ("::"")
77-
let closure_fold: String = path.segments.iter().map(|x| x.ident.as_str().to_string()).collect::<Vec<String>>().join("::");
78-
//Span the body of the closure (the |...| bit) and suggest the fix by taking the error and encapsulating it in the enum
77+
// finally get the idents for each path segment collect them as a string and
78+
// join them with the path separator ("::"")
79+
let closure_fold: String = path
80+
.segments
81+
.iter()
82+
.map(|x| x.ident.as_str().to_string())
83+
.collect::<Vec<String>>()
84+
.join("::");
85+
//Span the body of the closure (the |...| bit) and suggest the fix by taking
86+
// the error and encapsulating it in the enum
7987
span_lint_and_sugg(
8088
cx,
8189
MAP_ERR_IGNORE,
@@ -84,10 +92,11 @@ impl<'tcx> LateLintPass<'tcx> for MapErrIgnore {
8492
"Allow the error enum to encapsulate the original error",
8593
format!("|e| {}(e)", closure_fold),
8694
Applicability::HasPlaceholders,
87-
);
95+
);
8896
}
8997
} else {
90-
//If we cannot build the enum in a human readable way just suggest not throwing way the error
98+
//If we cannot build the enum in a human readable way just suggest not throwing way
99+
// the error
91100
span_lint_and_sugg(
92101
cx,
93102
MAP_ERR_IGNORE,
@@ -96,13 +105,13 @@ impl<'tcx> LateLintPass<'tcx> for MapErrIgnore {
96105
"Allow the error enum to encapsulate the original error",
97106
"|e|".to_string(),
98107
Applicability::HasPlaceholders,
99-
);
108+
);
100109
}
101110
}
102111
}
103-
}
112+
}
104113
}
105114
}
106115
}
107116
}
108-
}
117+
}

tests/ui/drop_ref.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#![warn(clippy::drop_ref)]
22
#![allow(clippy::toplevel_ref_arg)]
3+
#![allow(clippy::map_err_ignore)]
34

45
use std::mem::drop;
56

tests/ui/drop_ref.stderr

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,108 +1,108 @@
11
error: calls to `std::mem::drop` with a reference instead of an owned value. Dropping a reference does nothing.
2-
--> $DIR/drop_ref.rs:9:5
2+
--> $DIR/drop_ref.rs:10:5
33
|
44
LL | drop(&SomeStruct);
55
| ^^^^^^^^^^^^^^^^^
66
|
77
= note: `-D clippy::drop-ref` implied by `-D warnings`
88
note: argument has type `&SomeStruct`
9-
--> $DIR/drop_ref.rs:9:10
9+
--> $DIR/drop_ref.rs:10:10
1010
|
1111
LL | drop(&SomeStruct);
1212
| ^^^^^^^^^^^
1313

1414
error: calls to `std::mem::drop` with a reference instead of an owned value. Dropping a reference does nothing.
15-
--> $DIR/drop_ref.rs:12:5
15+
--> $DIR/drop_ref.rs:13:5
1616
|
1717
LL | drop(&owned1);
1818
| ^^^^^^^^^^^^^
1919
|
2020
note: argument has type `&SomeStruct`
21-
--> $DIR/drop_ref.rs:12:10
21+
--> $DIR/drop_ref.rs:13:10
2222
|
2323
LL | drop(&owned1);
2424
| ^^^^^^^
2525

2626
error: calls to `std::mem::drop` with a reference instead of an owned value. Dropping a reference does nothing.
27-
--> $DIR/drop_ref.rs:13:5
27+
--> $DIR/drop_ref.rs:14:5
2828
|
2929
LL | drop(&&owned1);
3030
| ^^^^^^^^^^^^^^
3131
|
3232
note: argument has type `&&SomeStruct`
33-
--> $DIR/drop_ref.rs:13:10
33+
--> $DIR/drop_ref.rs:14:10
3434
|
3535
LL | drop(&&owned1);
3636
| ^^^^^^^^
3737

3838
error: calls to `std::mem::drop` with a reference instead of an owned value. Dropping a reference does nothing.
39-
--> $DIR/drop_ref.rs:14:5
39+
--> $DIR/drop_ref.rs:15:5
4040
|
4141
LL | drop(&mut owned1);
4242
| ^^^^^^^^^^^^^^^^^
4343
|
4444
note: argument has type `&mut SomeStruct`
45-
--> $DIR/drop_ref.rs:14:10
45+
--> $DIR/drop_ref.rs:15:10
4646
|
4747
LL | drop(&mut owned1);
4848
| ^^^^^^^^^^^
4949

5050
error: calls to `std::mem::drop` with a reference instead of an owned value. Dropping a reference does nothing.
51-
--> $DIR/drop_ref.rs:18:5
51+
--> $DIR/drop_ref.rs:19:5
5252
|
5353
LL | drop(reference1);
5454
| ^^^^^^^^^^^^^^^^
5555
|
5656
note: argument has type `&SomeStruct`
57-
--> $DIR/drop_ref.rs:18:10
57+
--> $DIR/drop_ref.rs:19:10
5858
|
5959
LL | drop(reference1);
6060
| ^^^^^^^^^^
6161

6262
error: calls to `std::mem::drop` with a reference instead of an owned value. Dropping a reference does nothing.
63-
--> $DIR/drop_ref.rs:21:5
63+
--> $DIR/drop_ref.rs:22:5
6464
|
6565
LL | drop(reference2);
6666
| ^^^^^^^^^^^^^^^^
6767
|
6868
note: argument has type `&mut SomeStruct`
69-
--> $DIR/drop_ref.rs:21:10
69+
--> $DIR/drop_ref.rs:22:10
7070
|
7171
LL | drop(reference2);
7272
| ^^^^^^^^^^
7373

7474
error: calls to `std::mem::drop` with a reference instead of an owned value. Dropping a reference does nothing.
75-
--> $DIR/drop_ref.rs:24:5
75+
--> $DIR/drop_ref.rs:25:5
7676
|
7777
LL | drop(reference3);
7878
| ^^^^^^^^^^^^^^^^
7979
|
8080
note: argument has type `&SomeStruct`
81-
--> $DIR/drop_ref.rs:24:10
81+
--> $DIR/drop_ref.rs:25:10
8282
|
8383
LL | drop(reference3);
8484
| ^^^^^^^^^^
8585

8686
error: calls to `std::mem::drop` with a reference instead of an owned value. Dropping a reference does nothing.
87-
--> $DIR/drop_ref.rs:29:5
87+
--> $DIR/drop_ref.rs:30:5
8888
|
8989
LL | drop(&val);
9090
| ^^^^^^^^^^
9191
|
9292
note: argument has type `&T`
93-
--> $DIR/drop_ref.rs:29:10
93+
--> $DIR/drop_ref.rs:30:10
9494
|
9595
LL | drop(&val);
9696
| ^^^^
9797

9898
error: calls to `std::mem::drop` with a reference instead of an owned value. Dropping a reference does nothing.
99-
--> $DIR/drop_ref.rs:37:5
99+
--> $DIR/drop_ref.rs:38:5
100100
|
101101
LL | std::mem::drop(&SomeStruct);
102102
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
103103
|
104104
note: argument has type `&SomeStruct`
105-
--> $DIR/drop_ref.rs:37:20
105+
--> $DIR/drop_ref.rs:38:20
106106
|
107107
LL | std::mem::drop(&SomeStruct);
108108
| ^^^^^^^^^^^

tests/ui/map_err.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
use std::convert::TryFrom;
2+
use std::error::Error;
3+
use std::fmt;
4+
5+
#[derive(Debug)]
6+
enum Errors {
7+
Ignored,
8+
}
9+
10+
impl Error for Errors {}
11+
12+
impl fmt::Display for Errors {
13+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
14+
write!(f, "Error")
15+
}
16+
}
17+
18+
fn main() -> Result<(), Errors> {
19+
let x = u32::try_from(-123_i32);
20+
21+
println!("{:?}", x.map_err(|_| Errors::Ignored));
22+
23+
Ok(())
24+
}

tests/ui/map_err.stderr

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
error: `map_err` has thrown away the original error
2+
--> $DIR/map_err.rs:21:32
3+
|
4+
LL | println!("{:?}", x.map_err(|_| Errors::Ignored));
5+
| ^^^ help: Allow the error enum to encapsulate the original error: `|e| Errors::Ignored(e)`
6+
|
7+
= note: `-D clippy::map-err-ignore` implied by `-D warnings`
8+
9+
error: aborting due to previous error
10+

0 commit comments

Comments
 (0)