Skip to content

Commit 68e7356

Browse files
committed
Switch to for_each_expr in some lints
1 parent e604043 commit 68e7356

9 files changed

+77
-17
lines changed

clippy_lints/src/missing_fields_in_debug.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::ops::ControlFlow;
33
use clippy_utils::diagnostics::span_lint_and_then;
44
use clippy_utils::is_path_lang_item;
55
use clippy_utils::ty::is_type_diagnostic_item;
6-
use clippy_utils::visitors::{for_each_expr_without_closures, Visitable};
6+
use clippy_utils::visitors::{for_each_expr, Visitable};
77
use rustc_ast::LitKind;
88
use rustc_data_structures::fx::FxHashSet;
99
use rustc_hir::def::{DefKind, Res};
@@ -110,7 +110,7 @@ fn should_lint<'tcx>(
110110
// Is there a call to `DebugStruct::debug_struct`? Do lint if there is.
111111
let mut has_debug_struct = false;
112112

113-
for_each_expr_without_closures(block, |expr| {
113+
for_each_expr(cx, block, |expr| {
114114
if let ExprKind::MethodCall(path, recv, ..) = &expr.kind {
115115
let recv_ty = typeck_results.expr_ty(recv).peel_refs();
116116

@@ -165,7 +165,7 @@ fn check_struct<'tcx>(
165165
let mut has_direct_field_access = false;
166166
let mut field_accesses = FxHashSet::default();
167167

168-
for_each_expr_without_closures(block, |expr| {
168+
for_each_expr(cx, block, |expr| {
169169
if let ExprKind::Field(target, ident) = expr.kind
170170
&& let target_ty = typeck_results.expr_ty_adjusted(target).peel_refs()
171171
&& target_ty == self_ty

clippy_lints/src/panic_in_result_fn.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use clippy_utils::diagnostics::span_lint_and_then;
22
use clippy_utils::macros::root_macro_call_first_node;
33
use clippy_utils::return_ty;
44
use clippy_utils::ty::is_type_diagnostic_item;
5-
use clippy_utils::visitors::{for_each_expr_without_closures, Descend};
5+
use clippy_utils::visitors::{for_each_expr, Descend};
66
use core::ops::ControlFlow;
77
use rustc_hir as hir;
88
use rustc_hir::intravisit::FnKind;
@@ -64,7 +64,7 @@ impl<'tcx> LateLintPass<'tcx> for PanicInResultFn {
6464

6565
fn lint_impl_body<'tcx>(cx: &LateContext<'tcx>, impl_span: Span, body: &'tcx hir::Body<'tcx>) {
6666
let mut panics = Vec::new();
67-
let _: Option<!> = for_each_expr_without_closures(body.value, |e| {
67+
let _: Option<!> = for_each_expr(cx, body.value, |e| {
6868
let Some(macro_call) = root_macro_call_first_node(cx, e) else {
6969
return ControlFlow::Continue(Descend::Yes);
7070
};

clippy_lints/src/unwrap_in_result.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use clippy_utils::diagnostics::span_lint_and_then;
22
use clippy_utils::ty::is_type_diagnostic_item;
3-
use clippy_utils::visitors::for_each_expr_without_closures;
3+
use clippy_utils::visitors::for_each_expr;
44
use clippy_utils::{method_chain_args, return_ty};
55
use core::ops::ControlFlow;
66
use rustc_hir as hir;
@@ -75,7 +75,7 @@ fn lint_impl_body<'tcx>(cx: &LateContext<'tcx>, impl_span: Span, impl_item: &'tc
7575
let body = cx.tcx.hir().body(body_id);
7676
let typeck = cx.tcx.typeck(impl_item.owner_id.def_id);
7777
let mut result = Vec::new();
78-
let _: Option<!> = for_each_expr_without_closures(body.value, |e| {
78+
let _: Option<!> = for_each_expr(cx, body.value, |e| {
7979
// check for `expect`
8080
if let Some(arglists) = method_chain_args(e, &["expect"]) {
8181
let receiver_ty = typeck.expr_ty(arglists[0].0).peel_refs();

tests/ui/missing_fields_in_debug.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
use std::fmt;
55
use std::marker::PhantomData;
66
use std::ops::Deref;
7+
use std::thread::LocalKey;
78

89
struct NamedStruct1Ignored {
910
data: u8,
@@ -191,4 +192,21 @@ impl fmt::Debug for WithPD {
191192
}
192193
}
193194

195+
struct InClosure {
196+
a: u8,
197+
b: String,
198+
}
199+
200+
impl fmt::Debug for InClosure {
201+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
202+
let mut d = f.debug_struct("InClosure");
203+
d.field("a", &self.a);
204+
let mut c = || {
205+
d.field("b", &self.b);
206+
};
207+
c();
208+
d.finish()
209+
}
210+
}
211+
194212
fn main() {}

tests/ui/missing_fields_in_debug.stderr

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: manual `Debug` impl does not include all fields
2-
--> tests/ui/missing_fields_in_debug.rs:13:1
2+
--> tests/ui/missing_fields_in_debug.rs:14:1
33
|
44
LL | / impl fmt::Debug for NamedStruct1Ignored {
55
LL | |
@@ -11,7 +11,7 @@ LL | | }
1111
| |_^
1212
|
1313
note: this field is unused
14-
--> tests/ui/missing_fields_in_debug.rs:10:5
14+
--> tests/ui/missing_fields_in_debug.rs:11:5
1515
|
1616
LL | hidden: u32,
1717
| ^^^^^^^^^^^
@@ -21,7 +21,7 @@ LL | hidden: u32,
2121
= help: to override `-D warnings` add `#[allow(clippy::missing_fields_in_debug)]`
2222

2323
error: manual `Debug` impl does not include all fields
24-
--> tests/ui/missing_fields_in_debug.rs:32:1
24+
--> tests/ui/missing_fields_in_debug.rs:33:1
2525
|
2626
LL | / impl fmt::Debug for NamedStructMultipleIgnored {
2727
LL | |
@@ -33,25 +33,25 @@ LL | | }
3333
| |_^
3434
|
3535
note: this field is unused
36-
--> tests/ui/missing_fields_in_debug.rs:26:5
36+
--> tests/ui/missing_fields_in_debug.rs:27:5
3737
|
3838
LL | hidden: u32,
3939
| ^^^^^^^^^^^
4040
note: this field is unused
41-
--> tests/ui/missing_fields_in_debug.rs:27:5
41+
--> tests/ui/missing_fields_in_debug.rs:28:5
4242
|
4343
LL | hidden2: String,
4444
| ^^^^^^^^^^^^^^^
4545
note: this field is unused
46-
--> tests/ui/missing_fields_in_debug.rs:29:5
46+
--> tests/ui/missing_fields_in_debug.rs:30:5
4747
|
4848
LL | hidden4: ((((u8), u16), u32), u64),
4949
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
5050
= help: consider including all fields in this `Debug` impl
5151
= help: consider calling `.finish_non_exhaustive()` if you intend to ignore fields
5252

5353
error: manual `Debug` impl does not include all fields
54-
--> tests/ui/missing_fields_in_debug.rs:94:1
54+
--> tests/ui/missing_fields_in_debug.rs:95:1
5555
|
5656
LL | / impl fmt::Debug for MultiExprDebugImpl {
5757
LL | |
@@ -63,7 +63,7 @@ LL | | }
6363
| |_^
6464
|
6565
note: this field is unused
66-
--> tests/ui/missing_fields_in_debug.rs:90:5
66+
--> tests/ui/missing_fields_in_debug.rs:91:5
6767
|
6868
LL | b: String,
6969
| ^^^^^^^^^

tests/ui/panic_in_result_fn.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,11 @@ fn function_result_with_panic() -> Result<bool, String> // should emit lint
5656
panic!("error");
5757
}
5858

59+
fn in_closure() -> Result<bool, String> {
60+
let c = || panic!();
61+
c()
62+
}
63+
5964
fn todo() {
6065
println!("something");
6166
}

tests/ui/panic_in_result_fn.stderr

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,5 +34,21 @@ note: return Err() instead of panicking
3434
LL | panic!("error");
3535
| ^^^^^^^^^^^^^^^
3636

37-
error: aborting due to 2 previous errors
37+
error: used `panic!()` or assertion in a function that returns `Result`
38+
--> tests/ui/panic_in_result_fn.rs:59:1
39+
|
40+
LL | / fn in_closure() -> Result<bool, String> {
41+
LL | | let c = || panic!();
42+
LL | | c()
43+
LL | | }
44+
| |_^
45+
|
46+
= help: `panic!()` or assertions should not be used in a function that returns `Result` as `Result` is expected to return an error instead of crashing
47+
note: return Err() instead of panicking
48+
--> tests/ui/panic_in_result_fn.rs:60:16
49+
|
50+
LL | let c = || panic!();
51+
| ^^^^^^^^
52+
53+
error: aborting due to 3 previous errors
3854

tests/ui/unwrap_in_result.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,11 @@ impl A {
3838
}
3939
None
4040
}
41+
42+
fn in_closure(a: Option<bool>) -> Option<bool> {
43+
let c = || a.unwrap();
44+
Some(c())
45+
}
4146
}
4247

4348
fn main() {

tests/ui/unwrap_in_result.stderr

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,5 +38,21 @@ note: potential non-recoverable error(s)
3838
LL | let i = i_str.parse::<i32>().expect("not a number");
3939
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
4040

41-
error: aborting due to 2 previous errors
41+
error: used unwrap or expect in a function that returns result or option
42+
--> tests/ui/unwrap_in_result.rs:42:5
43+
|
44+
LL | / fn in_closure(a: Option<bool>) -> Option<bool> {
45+
LL | | let c = || a.unwrap();
46+
LL | | Some(c())
47+
LL | | }
48+
| |_____^
49+
|
50+
= help: unwrap and expect should not be used in a function that returns result or option
51+
note: potential non-recoverable error(s)
52+
--> tests/ui/unwrap_in_result.rs:43:20
53+
|
54+
LL | let c = || a.unwrap();
55+
| ^^^^^^^^^^
56+
57+
error: aborting due to 3 previous errors
4258

0 commit comments

Comments
 (0)