Skip to content

Commit 41e94dd

Browse files
authored
Merge pull request #3285 from devonhollowood/pedantic-dogfood-items-after-statements
Pedantic dogfood: `items_after_statements`
2 parents 63ceabf + 82638e4 commit 41e94dd

File tree

4 files changed

+39
-37
lines changed

4 files changed

+39
-37
lines changed

clippy_lints/src/consts.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,8 @@ impl<'c, 'cc> ConstEvalLateContext<'c, 'cc> {
275275

276276
/// lookup a possibly constant expression from a ExprKind::Path
277277
fn fetch_path(&mut self, qpath: &QPath, id: HirId) -> Option<Constant> {
278+
use crate::rustc::mir::interpret::GlobalId;
279+
278280
let def = self.tables.qpath_def(qpath, id);
279281
match def {
280282
Def::Const(def_id) | Def::AssociatedConst(def_id) => {
@@ -289,7 +291,7 @@ impl<'c, 'cc> ConstEvalLateContext<'c, 'cc> {
289291
instance,
290292
promoted: None,
291293
};
292-
use crate::rustc::mir::interpret::GlobalId;
294+
293295
let result = self.tcx.const_eval(self.param_env.and(gid)).ok()?;
294296
let ret = miri_to_const(self.tcx, result);
295297
if ret.is_some() {

clippy_lints/src/methods/mod.rs

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1102,18 +1102,6 @@ fn lint_expect_fun_call(cx: &LateContext<'_, '_>, expr: &hir::Expr, method_span:
11021102
arg: &hir::Expr,
11031103
span: Span,
11041104
) {
1105-
if name != "expect" {
1106-
return;
1107-
}
1108-
1109-
let self_type = cx.tables.expr_ty(self_expr);
1110-
let known_types = &[&paths::OPTION, &paths::RESULT];
1111-
1112-
// if not a known type, return early
1113-
if known_types.iter().all(|&k| !match_type(cx, self_type, k)) {
1114-
return;
1115-
}
1116-
11171105
fn is_call(node: &hir::ExprKind) -> bool {
11181106
match node {
11191107
hir::ExprKind::AddrOf(_, expr) => {
@@ -1128,6 +1116,18 @@ fn lint_expect_fun_call(cx: &LateContext<'_, '_>, expr: &hir::Expr, method_span:
11281116
}
11291117
}
11301118

1119+
if name != "expect" {
1120+
return;
1121+
}
1122+
1123+
let self_type = cx.tables.expr_ty(self_expr);
1124+
let known_types = &[&paths::OPTION, &paths::RESULT];
1125+
1126+
// if not a known type, return early
1127+
if known_types.iter().all(|&k| !match_type(cx, self_type, k)) {
1128+
return;
1129+
}
1130+
11311131
if !is_call(&arg.node) {
11321132
return;
11331133
}
@@ -1359,14 +1359,6 @@ fn lint_iter_cloned_collect(cx: &LateContext<'_, '_>, expr: &hir::Expr, iter_arg
13591359
}
13601360

13611361
fn lint_unnecessary_fold(cx: &LateContext<'_, '_>, expr: &hir::Expr, fold_args: &[hir::Expr]) {
1362-
// Check that this is a call to Iterator::fold rather than just some function called fold
1363-
if !match_trait_method(cx, expr, &paths::ITERATOR) {
1364-
return;
1365-
}
1366-
1367-
assert!(fold_args.len() == 3,
1368-
"Expected fold_args to have three entries - the receiver, the initial value and the closure");
1369-
13701362
fn check_fold_with_op(
13711363
cx: &LateContext<'_, '_>,
13721364
fold_args: &[hir::Expr],
@@ -1423,6 +1415,14 @@ fn lint_unnecessary_fold(cx: &LateContext<'_, '_>, expr: &hir::Expr, fold_args:
14231415
}
14241416
}
14251417

1418+
// Check that this is a call to Iterator::fold rather than just some function called fold
1419+
if !match_trait_method(cx, expr, &paths::ITERATOR) {
1420+
return;
1421+
}
1422+
1423+
assert!(fold_args.len() == 3,
1424+
"Expected fold_args to have three entries - the receiver, the initial value and the closure");
1425+
14261426
// Check if the first argument to .fold is a suitable literal
14271427
match fold_args[1].node {
14281428
hir::ExprKind::Lit(ref lit) => {

clippy_lints/src/utils/higher.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,14 @@ pub struct Range<'a> {
5656

5757
/// Higher a `hir` range to something similar to `ast::ExprKind::Range`.
5858
pub fn range<'a, 'b, 'tcx>(cx: &LateContext<'a, 'tcx>, expr: &'b hir::Expr) -> Option<Range<'b>> {
59+
/// Find the field named `name` in the field. Always return `Some` for
60+
/// convenience.
61+
fn get_field<'a>(name: &str, fields: &'a [hir::Field]) -> Option<&'a hir::Expr> {
62+
let expr = &fields.iter().find(|field| field.ident.name == name)?.expr;
63+
64+
Some(expr)
65+
}
66+
5967

6068
let def_path = match cx.tables.expr_ty(expr).sty {
6169
ty::Adt(def, _) => cx.tcx.def_path(def.did),
@@ -85,14 +93,6 @@ pub fn range<'a, 'b, 'tcx>(cx: &LateContext<'a, 'tcx>, expr: &'b hir::Expr) -> O
8593
return None;
8694
}
8795

88-
/// Find the field named `name` in the field. Always return `Some` for
89-
/// convenience.
90-
fn get_field<'a>(name: &str, fields: &'a [hir::Field]) -> Option<&'a hir::Expr> {
91-
let expr = &fields.iter().find(|field| field.ident.name == name)?.expr;
92-
93-
Some(expr)
94-
}
95-
9696
// The range syntax is expanded to literal paths starting with `core` or `std`
9797
// depending on
9898
// `#[no_std]`. Testing both instead of resolving the paths.

clippy_lints/src/write.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,7 @@ impl EarlyLintPass for Pass {
256256
}
257257

258258
fn check_tts<'a>(cx: &EarlyContext<'a>, tts: &ThinTokenStream, is_write: bool) -> (Option<String>, Option<Expr>) {
259+
use crate::fmt_macros::*;
259260
let tts = TokenStream::from(tts.clone());
260261
let mut parser = parser::Parser::new(&cx.sess.parse_sess, tts, None, false, false);
261262
let mut expr: Option<Expr> = None;
@@ -274,7 +275,6 @@ fn check_tts<'a>(cx: &EarlyContext<'a>, tts: &ThinTokenStream, is_write: bool) -
274275
Ok(token) => token.0.to_string(),
275276
Err(_) => return (None, expr),
276277
};
277-
use crate::fmt_macros::*;
278278
let tmp = fmtstr.clone();
279279
let mut args = vec![];
280280
let mut fmt_parser = Parser::new(&tmp, None);
@@ -293,13 +293,6 @@ fn check_tts<'a>(cx: &EarlyContext<'a>, tts: &ThinTokenStream, is_write: bool) -
293293
let lint = if is_write { WRITE_LITERAL } else { PRINT_LITERAL };
294294
let mut idx = 0;
295295
loop {
296-
if !parser.eat(&token::Comma) {
297-
return (Some(fmtstr), expr);
298-
}
299-
let token_expr = match parser.parse_expr().map_err(|mut err| err.cancel()) {
300-
Ok(expr) => expr,
301-
Err(_) => return (Some(fmtstr), None),
302-
};
303296
const SIMPLE: FormatSpec<'_> = FormatSpec {
304297
fill: None,
305298
align: AlignUnknown,
@@ -308,6 +301,13 @@ fn check_tts<'a>(cx: &EarlyContext<'a>, tts: &ThinTokenStream, is_write: bool) -
308301
width: CountImplied,
309302
ty: "",
310303
};
304+
if !parser.eat(&token::Comma) {
305+
return (Some(fmtstr), expr);
306+
}
307+
let token_expr = match parser.parse_expr().map_err(|mut err| err.cancel()) {
308+
Ok(expr) => expr,
309+
Err(_) => return (Some(fmtstr), None),
310+
};
311311
match &token_expr.node {
312312
ExprKind::Lit(_) => {
313313
let mut all_simple = true;

0 commit comments

Comments
 (0)