diff --git a/clippy_lints/src/consts.rs b/clippy_lints/src/consts.rs index 584060aeca38..2422e8e8a10f 100644 --- a/clippy_lints/src/consts.rs +++ b/clippy_lints/src/consts.rs @@ -275,6 +275,8 @@ impl<'c, 'cc> ConstEvalLateContext<'c, 'cc> { /// lookup a possibly constant expression from a ExprKind::Path fn fetch_path(&mut self, qpath: &QPath, id: HirId) -> Option { + use crate::rustc::mir::interpret::GlobalId; + let def = self.tables.qpath_def(qpath, id); match def { Def::Const(def_id) | Def::AssociatedConst(def_id) => { @@ -289,7 +291,7 @@ impl<'c, 'cc> ConstEvalLateContext<'c, 'cc> { instance, promoted: None, }; - use crate::rustc::mir::interpret::GlobalId; + let result = self.tcx.const_eval(self.param_env.and(gid)).ok()?; let ret = miri_to_const(self.tcx, result); if ret.is_some() { diff --git a/clippy_lints/src/methods/mod.rs b/clippy_lints/src/methods/mod.rs index d49a05f5186a..a5102824b1c7 100644 --- a/clippy_lints/src/methods/mod.rs +++ b/clippy_lints/src/methods/mod.rs @@ -1102,18 +1102,6 @@ fn lint_expect_fun_call(cx: &LateContext<'_, '_>, expr: &hir::Expr, method_span: arg: &hir::Expr, span: Span, ) { - if name != "expect" { - return; - } - - let self_type = cx.tables.expr_ty(self_expr); - let known_types = &[&paths::OPTION, &paths::RESULT]; - - // if not a known type, return early - if known_types.iter().all(|&k| !match_type(cx, self_type, k)) { - return; - } - fn is_call(node: &hir::ExprKind) -> bool { match node { hir::ExprKind::AddrOf(_, expr) => { @@ -1128,6 +1116,18 @@ fn lint_expect_fun_call(cx: &LateContext<'_, '_>, expr: &hir::Expr, method_span: } } + if name != "expect" { + return; + } + + let self_type = cx.tables.expr_ty(self_expr); + let known_types = &[&paths::OPTION, &paths::RESULT]; + + // if not a known type, return early + if known_types.iter().all(|&k| !match_type(cx, self_type, k)) { + return; + } + if !is_call(&arg.node) { return; } @@ -1359,14 +1359,6 @@ fn lint_iter_cloned_collect(cx: &LateContext<'_, '_>, expr: &hir::Expr, iter_arg } fn lint_unnecessary_fold(cx: &LateContext<'_, '_>, expr: &hir::Expr, fold_args: &[hir::Expr]) { - // Check that this is a call to Iterator::fold rather than just some function called fold - if !match_trait_method(cx, expr, &paths::ITERATOR) { - return; - } - - assert!(fold_args.len() == 3, - "Expected fold_args to have three entries - the receiver, the initial value and the closure"); - fn check_fold_with_op( cx: &LateContext<'_, '_>, fold_args: &[hir::Expr], @@ -1423,6 +1415,14 @@ fn lint_unnecessary_fold(cx: &LateContext<'_, '_>, expr: &hir::Expr, fold_args: } } + // Check that this is a call to Iterator::fold rather than just some function called fold + if !match_trait_method(cx, expr, &paths::ITERATOR) { + return; + } + + assert!(fold_args.len() == 3, + "Expected fold_args to have three entries - the receiver, the initial value and the closure"); + // Check if the first argument to .fold is a suitable literal match fold_args[1].node { hir::ExprKind::Lit(ref lit) => { diff --git a/clippy_lints/src/utils/higher.rs b/clippy_lints/src/utils/higher.rs index 584a6df1cc6f..29c7260f4912 100644 --- a/clippy_lints/src/utils/higher.rs +++ b/clippy_lints/src/utils/higher.rs @@ -56,6 +56,14 @@ pub struct Range<'a> { /// Higher a `hir` range to something similar to `ast::ExprKind::Range`. pub fn range<'a, 'b, 'tcx>(cx: &LateContext<'a, 'tcx>, expr: &'b hir::Expr) -> Option> { + /// Find the field named `name` in the field. Always return `Some` for + /// convenience. + fn get_field<'a>(name: &str, fields: &'a [hir::Field]) -> Option<&'a hir::Expr> { + let expr = &fields.iter().find(|field| field.ident.name == name)?.expr; + + Some(expr) + } + let def_path = match cx.tables.expr_ty(expr).sty { 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 return None; } - /// Find the field named `name` in the field. Always return `Some` for - /// convenience. - fn get_field<'a>(name: &str, fields: &'a [hir::Field]) -> Option<&'a hir::Expr> { - let expr = &fields.iter().find(|field| field.ident.name == name)?.expr; - - Some(expr) - } - // The range syntax is expanded to literal paths starting with `core` or `std` // depending on // `#[no_std]`. Testing both instead of resolving the paths. diff --git a/clippy_lints/src/write.rs b/clippy_lints/src/write.rs index 05ddbfe6f846..f84362fdbbc6 100644 --- a/clippy_lints/src/write.rs +++ b/clippy_lints/src/write.rs @@ -256,6 +256,7 @@ impl EarlyLintPass for Pass { } fn check_tts<'a>(cx: &EarlyContext<'a>, tts: &ThinTokenStream, is_write: bool) -> (Option, Option) { + use crate::fmt_macros::*; let tts = TokenStream::from(tts.clone()); let mut parser = parser::Parser::new(&cx.sess.parse_sess, tts, None, false, false); let mut expr: Option = None; @@ -274,7 +275,6 @@ fn check_tts<'a>(cx: &EarlyContext<'a>, tts: &ThinTokenStream, is_write: bool) - Ok(token) => token.0.to_string(), Err(_) => return (None, expr), }; - use crate::fmt_macros::*; let tmp = fmtstr.clone(); let mut args = vec![]; let mut fmt_parser = Parser::new(&tmp, None); @@ -293,13 +293,6 @@ fn check_tts<'a>(cx: &EarlyContext<'a>, tts: &ThinTokenStream, is_write: bool) - let lint = if is_write { WRITE_LITERAL } else { PRINT_LITERAL }; let mut idx = 0; loop { - if !parser.eat(&token::Comma) { - return (Some(fmtstr), expr); - } - let token_expr = match parser.parse_expr().map_err(|mut err| err.cancel()) { - Ok(expr) => expr, - Err(_) => return (Some(fmtstr), None), - }; const SIMPLE: FormatSpec<'_> = FormatSpec { fill: None, align: AlignUnknown, @@ -308,6 +301,13 @@ fn check_tts<'a>(cx: &EarlyContext<'a>, tts: &ThinTokenStream, is_write: bool) - width: CountImplied, ty: "", }; + if !parser.eat(&token::Comma) { + return (Some(fmtstr), expr); + } + let token_expr = match parser.parse_expr().map_err(|mut err| err.cancel()) { + Ok(expr) => expr, + Err(_) => return (Some(fmtstr), None), + }; match &token_expr.node { ExprKind::Lit(_) => { let mut all_simple = true;