From 90a4293759ec51243967ce21ef01f7c424f2004f Mon Sep 17 00:00:00 2001 From: Philipp Hansch Date: Thu, 15 Aug 2019 17:42:05 +0200 Subject: [PATCH 1/9] Remove publishing things Clippy is no longer published via crates.io, so these docs and the script are not needed anymore. --- PUBLISH.md | 14 -------------- pre_publish.sh | 22 ---------------------- publish.files | 3 --- 3 files changed, 39 deletions(-) delete mode 100644 PUBLISH.md delete mode 100755 pre_publish.sh delete mode 100644 publish.files diff --git a/PUBLISH.md b/PUBLISH.md deleted file mode 100644 index b85605dc3..000000000 --- a/PUBLISH.md +++ /dev/null @@ -1,14 +0,0 @@ -Steps to publish a new Clippy version - -- Bump `package.version` in `./Cargo.toml` (no need to manually bump `dependencies.clippy_lints.version`). -- Write a changelog entry. -- Run `./pre_publish.sh` -- Review and commit all changed files -- `git push` -- Wait for Travis's approval. -- Merge. -- `cargo publish` in `./clippy_lints`. -- `cargo publish` in the root directory. -- `git pull`. -- `git tag -s v0.0.X -m "v0.0.X"`. -- `git push --tags`. diff --git a/pre_publish.sh b/pre_publish.sh deleted file mode 100755 index 3602f671e..000000000 --- a/pre_publish.sh +++ /dev/null @@ -1,22 +0,0 @@ -#!/bin/bash - -set -e - -./util/update_lints.py - -# add all changed files -git add . -git commit -m "Bump the version" - -set +e - -echo "Running \`cargo fmt\`.." - -cd clippy_lints && cargo fmt -- --write-mode=overwrite && cd .. -cargo fmt -- --write-mode=overwrite - -echo "Running tests to make sure \`cargo fmt\` did not break anything.." - -cargo test - -echo "If the tests passed, review and commit the formatting changes and remember to add a git tag." diff --git a/publish.files b/publish.files deleted file mode 100644 index 9ee50447f..000000000 --- a/publish.files +++ /dev/null @@ -1,3 +0,0 @@ - M Cargo.toml - M CHANGELOG.md - M clippy_lints/Cargo.toml From 781794d49f9a8f54d11d9b768d65c9fddb875066 Mon Sep 17 00:00:00 2001 From: Philipp Hansch Date: Thu, 15 Aug 2019 17:43:30 +0200 Subject: [PATCH 2/9] Remove util/update_lints.py This has been deprecated for ~10 months, so I think it's fine to remove now. --- util/update_lints.py | 10 ---------- 1 file changed, 10 deletions(-) delete mode 100755 util/update_lints.py diff --git a/util/update_lints.py b/util/update_lints.py deleted file mode 100755 index 1800fa05c..000000000 --- a/util/update_lints.py +++ /dev/null @@ -1,10 +0,0 @@ -#!/usr/bin/env python - -import sys - -def main(): - print('Error: Please use `util/dev` to update lints') - return 1 - -if __name__ == '__main__': - sys.exit(main()) From 65b906a8fced2f0fb8c56f8f6209e258406d8a36 Mon Sep 17 00:00:00 2001 From: Lzu Tao Date: Fri, 16 Aug 2019 09:30:38 +0700 Subject: [PATCH 3/9] Rustup "Remove `Spanned` from `{ast,hir}::FieldPat`" --- clippy_lints/src/copies.rs | 2 +- clippy_lints/src/dbg_macro.rs | 4 ++-- clippy_lints/src/misc_early.rs | 6 +++--- clippy_lints/src/non_expressive_names.rs | 4 ++-- clippy_lints/src/shadow.rs | 8 ++++---- clippy_lints/src/utils/inspector.rs | 6 +++--- clippy_lints/src/utils/mod.rs | 2 +- clippy_lints/src/write.rs | 20 ++++++++++---------- 8 files changed, 26 insertions(+), 26 deletions(-) diff --git a/clippy_lints/src/copies.rs b/clippy_lints/src/copies.rs index 3894b56c0..64239bb4d 100644 --- a/clippy_lints/src/copies.rs +++ b/clippy_lints/src/copies.rs @@ -300,7 +300,7 @@ fn bindings<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, pat: &Pat) -> FxHashMap { for pat in fields { - bindings_impl(cx, &pat.node.pat, map); + bindings_impl(cx, &pat.pat, map); } }, PatKind::Tuple(ref fields, _) => { diff --git a/clippy_lints/src/dbg_macro.rs b/clippy_lints/src/dbg_macro.rs index ff76b35fa..1dcdaac9b 100644 --- a/clippy_lints/src/dbg_macro.rs +++ b/clippy_lints/src/dbg_macro.rs @@ -31,8 +31,8 @@ declare_lint_pass!(DbgMacro => [DBG_MACRO]); impl EarlyLintPass for DbgMacro { fn check_mac(&mut self, cx: &EarlyContext<'_>, mac: &ast::Mac) { - if mac.node.path == sym!(dbg) { - if let Some(sugg) = tts_span(mac.node.tts.clone()).and_then(|span| snippet_opt(cx, span)) { + if mac.path == sym!(dbg) { + if let Some(sugg) = tts_span(mac.tts.clone()).and_then(|span| snippet_opt(cx, span)) { span_lint_and_sugg( cx, DBG_MACRO, diff --git a/clippy_lints/src/misc_early.rs b/clippy_lints/src/misc_early.rs index 7d6f29c52..b998d3f8a 100644 --- a/clippy_lints/src/misc_early.rs +++ b/clippy_lints/src/misc_early.rs @@ -234,7 +234,7 @@ impl EarlyLintPass for MiscEarlyLints { .name; for field in pfields { - if let PatKind::Wild = field.node.pat.node { + if let PatKind::Wild = field.pat.node { wilds += 1; } } @@ -252,7 +252,7 @@ impl EarlyLintPass for MiscEarlyLints { let mut normal = vec![]; for field in pfields { - match field.node.pat.node { + match field.pat.node { PatKind::Wild => {}, _ => { if let Ok(n) = cx.sess().source_map().span_to_snippet(field.span) { @@ -262,7 +262,7 @@ impl EarlyLintPass for MiscEarlyLints { } } for field in pfields { - if let PatKind::Wild = field.node.pat.node { + if let PatKind::Wild = field.pat.node { wilds -= 1; if wilds > 0 { span_lint( diff --git a/clippy_lints/src/non_expressive_names.rs b/clippy_lints/src/non_expressive_names.rs index a8c45166a..fc614b08a 100644 --- a/clippy_lints/src/non_expressive_names.rs +++ b/clippy_lints/src/non_expressive_names.rs @@ -131,8 +131,8 @@ impl<'a, 'tcx, 'b> Visitor<'tcx> for SimilarNamesNameVisitor<'a, 'tcx, 'b> { PatKind::Ident(_, ident, _) => self.check_ident(ident), PatKind::Struct(_, ref fields, _) => { for field in fields { - if !field.node.is_shorthand { - self.visit_pat(&field.node.pat); + if !field.is_shorthand { + self.visit_pat(&field.pat); } } }, diff --git a/clippy_lints/src/shadow.rs b/clippy_lints/src/shadow.rs index 0e75f85cc..b30f8d415 100644 --- a/clippy_lints/src/shadow.rs +++ b/clippy_lints/src/shadow.rs @@ -190,20 +190,20 @@ fn check_pat<'a, 'tcx>( if let Some(init_struct) = init { if let ExprKind::Struct(_, ref efields, _) = init_struct.node { for field in pfields { - let name = field.node.ident.name; + let name = field.ident.name; let efield = efields .iter() .find_map(|f| if f.ident.name == name { Some(&*f.expr) } else { None }); - check_pat(cx, &field.node.pat, efield, span, bindings); + check_pat(cx, &field.pat, efield, span, bindings); } } else { for field in pfields { - check_pat(cx, &field.node.pat, init, span, bindings); + check_pat(cx, &field.pat, init, span, bindings); } } } else { for field in pfields { - check_pat(cx, &field.node.pat, None, span, bindings); + check_pat(cx, &field.pat, None, span, bindings); } } }, diff --git a/clippy_lints/src/utils/inspector.rs b/clippy_lints/src/utils/inspector.rs index 809590c88..309b5165c 100644 --- a/clippy_lints/src/utils/inspector.rs +++ b/clippy_lints/src/utils/inspector.rs @@ -420,11 +420,11 @@ fn print_pat(cx: &LateContext<'_, '_>, pat: &hir::Pat, indent: usize) { println!("{}ignore leftover fields: {}", ind, ignore); println!("{}fields:", ind); for field in fields { - println!("{} field name: {}", ind, field.node.ident.name); - if field.node.is_shorthand { + println!("{} field name: {}", ind, field.ident.name); + if field.is_shorthand { println!("{} in shorthand notation", ind); } - print_pat(cx, &field.node.pat, indent + 1); + print_pat(cx, &field.pat, indent + 1); } }, hir::PatKind::TupleStruct(ref path, ref fields, opt_dots_position) => { diff --git a/clippy_lints/src/utils/mod.rs b/clippy_lints/src/utils/mod.rs index 9e45c453a..9981799ed 100644 --- a/clippy_lints/src/utils/mod.rs +++ b/clippy_lints/src/utils/mod.rs @@ -783,7 +783,7 @@ pub fn is_refutable(cx: &LateContext<'_, '_>, pat: &Pat) -> bool { if is_enum_variant(cx, qpath, pat.hir_id) { true } else { - are_refutable(cx, fields.iter().map(|field| &*field.node.pat)) + are_refutable(cx, fields.iter().map(|field| &*field.pat)) } }, PatKind::TupleStruct(ref qpath, ref pats, _) => { diff --git a/clippy_lints/src/write.rs b/clippy_lints/src/write.rs index 3608c0e61..bcf688c19 100644 --- a/clippy_lints/src/write.rs +++ b/clippy_lints/src/write.rs @@ -183,9 +183,9 @@ declare_lint_pass!(Write => [ impl EarlyLintPass for Write { fn check_mac(&mut self, cx: &EarlyContext<'_>, mac: &Mac) { - if mac.node.path == sym!(println) { + if mac.path == sym!(println) { span_lint(cx, PRINT_STDOUT, mac.span, "use of `println!`"); - if let (Some(fmt_str), _) = check_tts(cx, &mac.node.tts, false) { + if let (Some(fmt_str), _) = check_tts(cx, &mac.tts, false) { if fmt_str.contents.is_empty() { span_lint_and_sugg( cx, @@ -198,9 +198,9 @@ impl EarlyLintPass for Write { ); } } - } else if mac.node.path == sym!(print) { + } else if mac.path == sym!(print) { span_lint(cx, PRINT_STDOUT, mac.span, "use of `print!`"); - if let (Some(fmt_str), _) = check_tts(cx, &mac.node.tts, false) { + if let (Some(fmt_str), _) = check_tts(cx, &mac.tts, false) { if check_newlines(&fmt_str) { span_lint_and_then( cx, @@ -211,7 +211,7 @@ impl EarlyLintPass for Write { err.multipart_suggestion( "use `println!` instead", vec![ - (mac.node.path.span, String::from("println")), + (mac.path.span, String::from("println")), (fmt_str.newline_span(), String::new()), ], Applicability::MachineApplicable, @@ -220,8 +220,8 @@ impl EarlyLintPass for Write { ); } } - } else if mac.node.path == sym!(write) { - if let (Some(fmt_str), _) = check_tts(cx, &mac.node.tts, true) { + } else if mac.path == sym!(write) { + if let (Some(fmt_str), _) = check_tts(cx, &mac.tts, true) { if check_newlines(&fmt_str) { span_lint_and_then( cx, @@ -232,7 +232,7 @@ impl EarlyLintPass for Write { err.multipart_suggestion( "use `writeln!()` instead", vec![ - (mac.node.path.span, String::from("writeln")), + (mac.path.span, String::from("writeln")), (fmt_str.newline_span(), String::new()), ], Applicability::MachineApplicable, @@ -241,8 +241,8 @@ impl EarlyLintPass for Write { ) } } - } else if mac.node.path == sym!(writeln) { - if let (Some(fmt_str), expr) = check_tts(cx, &mac.node.tts, true) { + } else if mac.path == sym!(writeln) { + if let (Some(fmt_str), expr) = check_tts(cx, &mac.tts, true) { if fmt_str.contents.is_empty() { let mut applicability = Applicability::MachineApplicable; let suggestion = expr.map_or_else( From a68abc03a2a43455b56b8383001ab8282ee766ad Mon Sep 17 00:00:00 2001 From: "KRAAI, MATTHEW [VISUS]" Date: Fri, 16 Aug 2019 05:41:35 -0700 Subject: [PATCH 4/9] Remove "a" from single_match_else description --- clippy_lints/src/matches.rs | 4 ++-- src/lintlist/mod.rs | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/clippy_lints/src/matches.rs b/clippy_lints/src/matches.rs index 8a6f6970e..52265ae55 100644 --- a/clippy_lints/src/matches.rs +++ b/clippy_lints/src/matches.rs @@ -41,7 +41,7 @@ declare_clippy_lint! { } declare_clippy_lint! { - /// **What it does:** Checks for matches with a two arms where an `if let else` will + /// **What it does:** Checks for matches with two arms where an `if let else` will /// usually suffice. /// /// **Why is this bad?** Just readability – `if let` nests less than a `match`. @@ -76,7 +76,7 @@ declare_clippy_lint! { /// ``` pub SINGLE_MATCH_ELSE, pedantic, - "a match statement with a two arms where the second arm's pattern is a placeholder instead of a specific match pattern" + "a match statement with two arms where the second arm's pattern is a placeholder instead of a specific match pattern" } declare_clippy_lint! { diff --git a/src/lintlist/mod.rs b/src/lintlist/mod.rs index c02cdcdbd..0df837093 100644 --- a/src/lintlist/mod.rs +++ b/src/lintlist/mod.rs @@ -1676,7 +1676,7 @@ pub const ALL_LINTS: [Lint; 309] = [ Lint { name: "single_match_else", group: "pedantic", - desc: "a match statement with a two arms where the second arm\'s pattern is a placeholder instead of a specific match pattern", + desc: "a match statement with two arms where the second arm\'s pattern is a placeholder instead of a specific match pattern", deprecation: None, module: "matches", }, From f74d9db7f4f7a1dce08ff690de14c9bce0381baa Mon Sep 17 00:00:00 2001 From: "KRAAI, MATTHEW [VISUS]" Date: Fri, 16 Aug 2019 09:29:30 -0700 Subject: [PATCH 5/9] Use outer_expn_data instead of outer_expn_info --- clippy_lints/src/lib.rs | 4 +- clippy_lints/src/misc.rs | 10 +++-- clippy_lints/src/panic_unimplemented.rs | 6 ++- clippy_lints/src/ranges.rs | 12 ++++-- clippy_lints/src/returns.rs | 6 ++- clippy_lints/src/utils/internal_lints.rs | 33 ++++++++------- clippy_lints/src/utils/mod.rs | 41 ++++++++++++------- clippy_lints/src/vec.rs | 10 ++--- ...{outer_expn_info.rs => outer_expn_data.rs} | 2 +- tests/ui/outer_expn_data.stderr | 15 +++++++ tests/ui/outer_expn_info.stderr | 15 ------- 11 files changed, 89 insertions(+), 65 deletions(-) rename tests/ui/{outer_expn_info.rs => outer_expn_data.rs} (87%) create mode 100644 tests/ui/outer_expn_data.stderr delete mode 100644 tests/ui/outer_expn_info.stderr diff --git a/clippy_lints/src/lib.rs b/clippy_lints/src/lib.rs index c8dbf7770..3054f11c1 100644 --- a/clippy_lints/src/lib.rs +++ b/clippy_lints/src/lib.rs @@ -445,7 +445,7 @@ pub fn register_plugins(reg: &mut rustc_plugin::Registry<'_>, conf: &Conf) { reg.register_early_lint_pass(box utils::internal_lints::ClippyLintsInternal); reg.register_late_lint_pass(box utils::internal_lints::CompilerLintFunctions::new()); reg.register_late_lint_pass(box utils::internal_lints::LintWithoutLintPass::default()); - reg.register_late_lint_pass(box utils::internal_lints::OuterExpnInfoPass); + reg.register_late_lint_pass(box utils::internal_lints::OuterExpnDataPass); reg.register_late_lint_pass(box utils::inspector::DeepCodeInspector); reg.register_late_lint_pass(box utils::author::Author); reg.register_late_lint_pass(box types::Types); @@ -681,7 +681,7 @@ pub fn register_plugins(reg: &mut rustc_plugin::Registry<'_>, conf: &Conf) { utils::internal_lints::CLIPPY_LINTS_INTERNAL, utils::internal_lints::COMPILER_LINT_FUNCTIONS, utils::internal_lints::LINT_WITHOUT_LINT_PASS, - utils::internal_lints::OUTER_EXPN_EXPN_INFO, + utils::internal_lints::OUTER_EXPN_EXPN_DATA, ]); reg.register_lint_group("clippy::all", Some("clippy"), vec![ diff --git a/clippy_lints/src/misc.rs b/clippy_lints/src/misc.rs index f8a249829..a127431e2 100644 --- a/clippy_lints/src/misc.rs +++ b/clippy_lints/src/misc.rs @@ -625,13 +625,17 @@ fn is_used(cx: &LateContext<'_, '_>, expr: &Expr) -> bool { /// generated by `#[derive(...)]` or the like). fn in_attributes_expansion(expr: &Expr) -> bool { use syntax::ext::hygiene::MacroKind; - expr.span.ctxt().outer_expn_info().map_or(false, |info| { - if let ExpnKind::Macro(MacroKind::Attr, _) = info.kind { + if expr.span.from_expansion() { + let data = expr.span.ctxt().outer_expn_data(); + + if let ExpnKind::Macro(MacroKind::Attr, _) = data.kind { true } else { false } - }) + } else { + false + } } /// Tests whether `res` is a variable defined outside a macro. diff --git a/clippy_lints/src/panic_unimplemented.rs b/clippy_lints/src/panic_unimplemented.rs index 8327dbcc6..617ac2393 100644 --- a/clippy_lints/src/panic_unimplemented.rs +++ b/clippy_lints/src/panic_unimplemented.rs @@ -69,8 +69,10 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for PanicUnimplemented { fn get_outer_span(expr: &Expr) -> Span { if_chain! { - if let Some(first) = expr.span.ctxt().outer_expn_info(); - if let Some(second) = first.call_site.ctxt().outer_expn_info(); + if expr.span.from_expansion(); + let first = expr.span.ctxt().outer_expn_data(); + if first.call_site.from_expansion(); + let second = first.call_site.ctxt().outer_expn_data(); then { second.call_site } else { diff --git a/clippy_lints/src/ranges.rs b/clippy_lints/src/ranges.rs index a0391e336..a4ce129c9 100644 --- a/clippy_lints/src/ranges.rs +++ b/clippy_lints/src/ranges.rs @@ -147,10 +147,14 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Ranges { }) = higher::range(cx, expr); if let Some(y) = y_plus_one(end); then { - let span = expr.span - .ctxt() - .outer_expn_info() - .map_or(expr.span, |info| info.call_site); + let span = if expr.span.from_expansion() { + expr.span + .ctxt() + .outer_expn_data() + .call_site + } else { + expr.span + }; span_lint_and_then( cx, RANGE_PLUS_ONE, diff --git a/clippy_lints/src/returns.rs b/clippy_lints/src/returns.rs index 0f2084e81..8a8133bfa 100644 --- a/clippy_lints/src/returns.rs +++ b/clippy_lints/src/returns.rs @@ -317,7 +317,11 @@ fn attr_is_cfg(attr: &ast::Attribute) -> bool { // get the def site fn get_def(span: Span) -> Option { - span.ctxt().outer_expn_info().and_then(|info| Some(info.def_site)) + if span.from_expansion() { + Some(span.ctxt().outer_expn_data().def_site) + } else { + None + } } // is this expr a `()` unit? diff --git a/clippy_lints/src/utils/internal_lints.rs b/clippy_lints/src/utils/internal_lints.rs index 0dcac1121..9f4ea395d 100644 --- a/clippy_lints/src/utils/internal_lints.rs +++ b/clippy_lints/src/utils/internal_lints.rs @@ -76,26 +76,26 @@ declare_clippy_lint! { } declare_clippy_lint! { - /// **What it does:** Checks for calls to `cx.outer().expn_info()` and suggests to use - /// the `cx.outer_expn_info()` + /// **What it does:** Checks for calls to `cx.outer().expn_data()` and suggests to use + /// the `cx.outer_expn_data()` /// - /// **Why is this bad?** `cx.outer_expn_info()` is faster and more concise. + /// **Why is this bad?** `cx.outer_expn_data()` is faster and more concise. /// /// **Known problems:** None. /// /// **Example:** /// Bad: /// ```rust,ignore - /// expr.span.ctxt().outer().expn_info() + /// expr.span.ctxt().outer().expn_data() /// ``` /// /// Good: /// ```rust,ignore - /// expr.span.ctxt().outer_expn_info() + /// expr.span.ctxt().outer_expn_data() /// ``` - pub OUTER_EXPN_EXPN_INFO, + pub OUTER_EXPN_EXPN_DATA, internal, - "using `cx.outer_expn().expn_info()` instead of `cx.outer_expn_info()`" + "using `cx.outer_expn().expn_data()` instead of `cx.outer_expn_data()`" } declare_lint_pass!(ClippyLintsInternal => [CLIPPY_LINTS_INTERNAL]); @@ -182,9 +182,8 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for LintWithoutLintPass { // actual span that invoked `declare_tool_lint!`: let lint_span = lint_span .ctxt() - .outer_expn_info() - .map(|ei| ei.call_site) - .expect("unable to get call_site"); + .outer_expn_data() + .call_site; if !self.registered_lints.contains(lint_name) { span_lint( @@ -278,17 +277,17 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for CompilerLintFunctions { } } -pub struct OuterExpnInfoPass; +pub struct OuterExpnDataPass; -impl_lint_pass!(OuterExpnInfoPass => [OUTER_EXPN_EXPN_INFO]); +impl_lint_pass!(OuterExpnDataPass => [OUTER_EXPN_EXPN_DATA]); -impl<'a, 'tcx> LateLintPass<'a, 'tcx> for OuterExpnInfoPass { +impl<'a, 'tcx> LateLintPass<'a, 'tcx> for OuterExpnDataPass { fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx hir::Expr) { let (method_names, arg_lists) = method_calls(expr, 2); let method_names: Vec = method_names.iter().map(|s| s.as_str()).collect(); let method_names: Vec<&str> = method_names.iter().map(std::convert::AsRef::as_ref).collect(); if_chain! { - if let ["expn_info", "outer_expn"] = method_names.as_slice(); + if let ["expn_data", "outer_expn"] = method_names.as_slice(); let args = arg_lists[1]; if args.len() == 1; let self_arg = &args[0]; @@ -297,11 +296,11 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for OuterExpnInfoPass { then { span_lint_and_sugg( cx, - OUTER_EXPN_EXPN_INFO, + OUTER_EXPN_EXPN_DATA, expr.span.trim_start(self_arg.span).unwrap_or(expr.span), - "usage of `outer_expn().expn_info()`", + "usage of `outer_expn().expn_data()`", "try", - ".outer_expn_info()".to_string(), + ".outer_expn_data()".to_string(), Applicability::MachineApplicable, ); } diff --git a/clippy_lints/src/utils/mod.rs b/clippy_lints/src/utils/mod.rs index 9981799ed..a25f25041 100644 --- a/clippy_lints/src/utils/mod.rs +++ b/clippy_lints/src/utils/mod.rs @@ -92,15 +92,15 @@ pub fn in_constant(cx: &LateContext<'_, '_>, id: HirId) -> bool { } } -/// Returns `true` if this `expn_info` was expanded by any macro or desugaring +/// Returns `true` if this `span` was expanded by any macro or desugaring pub fn in_macro_or_desugar(span: Span) -> bool { - span.ctxt().outer_expn_info().is_some() + span.from_expansion() } -/// Returns `true` if this `expn_info` was expanded by any macro. +/// Returns `true` if this `span` was expanded by any macro. pub fn in_macro(span: Span) -> bool { - if let Some(info) = span.ctxt().outer_expn_info() { - if let ExpnKind::Desugaring(..) = info.kind { + if span.from_expansion() { + if let ExpnKind::Desugaring(..) = span.ctxt().outer_expn_data().kind { false } else { true @@ -686,12 +686,18 @@ pub fn is_adjusted(cx: &LateContext<'_, '_>, e: &Expr) -> bool { /// See also `is_direct_expn_of`. pub fn is_expn_of(mut span: Span, name: &str) -> Option { loop { - let span_name_span = span.ctxt().outer_expn_info().map(|ei| (ei.kind.descr(), ei.call_site)); + if span.from_expansion() { + let data = span.ctxt().outer_expn_data(); + let mac_name = data.kind.descr(); + let new_span = data.call_site; - match span_name_span { - Some((mac_name, new_span)) if mac_name.as_str() == name => return Some(new_span), - None => return None, - Some((_, new_span)) => span = new_span, + if mac_name.as_str() == name { + return Some(new_span); + } else { + span = new_span; + } + } else { + return None; } } } @@ -706,11 +712,18 @@ pub fn is_expn_of(mut span: Span, name: &str) -> Option { /// `bar!` by /// `is_direct_expn_of`. pub fn is_direct_expn_of(span: Span, name: &str) -> Option { - let span_name_span = span.ctxt().outer_expn_info().map(|ei| (ei.kind.descr(), ei.call_site)); + if span.from_expansion() { + let data = span.ctxt().outer_expn_data(); + let mac_name = data.kind.descr(); + let new_span = data.call_site; - match span_name_span { - Some((mac_name, new_span)) if mac_name.as_str() == name => Some(new_span), - _ => None, + if mac_name.as_str() == name { + Some(new_span) + } else { + None + } + } else { + None } } diff --git a/clippy_lints/src/vec.rs b/clippy_lints/src/vec.rs index c9a2eecd5..d7709784a 100644 --- a/clippy_lints/src/vec.rs +++ b/clippy_lints/src/vec.rs @@ -49,13 +49,11 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for UselessVec { // report the error around the `vec!` not inside `:` let span = arg.span .ctxt() - .outer_expn_info() - .map(|info| info.call_site) - .expect("unable to get call_site") + .outer_expn_data() + .call_site .ctxt() - .outer_expn_info() - .map(|info| info.call_site) - .expect("unable to get call_site"); + .outer_expn_data() + .call_site; check_vec_macro(cx, &vec_args, span); } } diff --git a/tests/ui/outer_expn_info.rs b/tests/ui/outer_expn_data.rs similarity index 87% rename from tests/ui/outer_expn_info.rs rename to tests/ui/outer_expn_data.rs index ed0542c92..15ac315a5 100644 --- a/tests/ui/outer_expn_info.rs +++ b/tests/ui/outer_expn_data.rs @@ -16,7 +16,7 @@ declare_lint_pass!(Pass => [TEST_LINT]); impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass { fn check_expr(&mut self, _cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr) { - let _ = expr.span.ctxt().outer_expn().expn_info(); + let _ = expr.span.ctxt().outer_expn().expn_data(); } } diff --git a/tests/ui/outer_expn_data.stderr b/tests/ui/outer_expn_data.stderr new file mode 100644 index 000000000..cd12ad79d --- /dev/null +++ b/tests/ui/outer_expn_data.stderr @@ -0,0 +1,15 @@ +error: usage of `outer_expn().expn_data()` + --> $DIR/outer_expn_data.rs:19:33 + | +LL | let _ = expr.span.ctxt().outer_expn().expn_data(); + | ^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `.outer_expn_data()` + | +note: lint level defined here + --> $DIR/outer_expn_data.rs:1:9 + | +LL | #![deny(clippy::internal)] + | ^^^^^^^^^^^^^^^^ + = note: `#[deny(clippy::outer_expn_expn_data)]` implied by `#[deny(clippy::internal)]` + +error: aborting due to previous error + diff --git a/tests/ui/outer_expn_info.stderr b/tests/ui/outer_expn_info.stderr deleted file mode 100644 index 4bbd1493d..000000000 --- a/tests/ui/outer_expn_info.stderr +++ /dev/null @@ -1,15 +0,0 @@ -error: usage of `outer_expn().expn_info()` - --> $DIR/outer_expn_info.rs:19:33 - | -LL | let _ = expr.span.ctxt().outer_expn().expn_info(); - | ^^^^^^^^^^^^^^^^^^^^^^^^^ help: try: `.outer_expn_info()` - | -note: lint level defined here - --> $DIR/outer_expn_info.rs:1:9 - | -LL | #![deny(clippy::internal)] - | ^^^^^^^^^^^^^^^^ - = note: `#[deny(clippy::outer_expn_expn_info)]` implied by `#[deny(clippy::internal)]` - -error: aborting due to previous error - From 2456b355f264c7b79a6f8c8f296e70b27e9bb6c9 Mon Sep 17 00:00:00 2001 From: flip1995 Date: Fri, 16 Aug 2019 19:05:44 +0200 Subject: [PATCH 6/9] Run rustfmt --- clippy_lints/src/utils/internal_lints.rs | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/clippy_lints/src/utils/internal_lints.rs b/clippy_lints/src/utils/internal_lints.rs index 9f4ea395d..855b1e6ef 100644 --- a/clippy_lints/src/utils/internal_lints.rs +++ b/clippy_lints/src/utils/internal_lints.rs @@ -180,10 +180,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for LintWithoutLintPass { // not able to capture the error. // Therefore, we need to climb the macro expansion tree and find the // actual span that invoked `declare_tool_lint!`: - let lint_span = lint_span - .ctxt() - .outer_expn_data() - .call_site; + let lint_span = lint_span.ctxt().outer_expn_data().call_site; if !self.registered_lints.contains(lint_name) { span_lint( From 439284b58400be98036117d053815eb24472fee7 Mon Sep 17 00:00:00 2001 From: Lzu Tao Date: Sun, 18 Aug 2019 01:46:44 +0700 Subject: [PATCH 7/9] Make code cleaner and more readable --- clippy_lints/src/assertions_on_constants.rs | 64 ++++++++++----------- 1 file changed, 31 insertions(+), 33 deletions(-) diff --git a/clippy_lints/src/assertions_on_constants.rs b/clippy_lints/src/assertions_on_constants.rs index 0f0c0dbc1..22dbd3ecc 100644 --- a/clippy_lints/src/assertions_on_constants.rs +++ b/clippy_lints/src/assertions_on_constants.rs @@ -34,39 +34,37 @@ declare_lint_pass!(AssertionsOnConstants => [ASSERTIONS_ON_CONSTANTS]); impl<'a, 'tcx> LateLintPass<'a, 'tcx> for AssertionsOnConstants { fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, e: &'tcx Expr) { let mut is_debug_assert = false; - let debug_assert_not_in_macro_or_desugar = |span: Span| { - is_debug_assert = true; - // Check that `debug_assert!` itself is not inside a macro - !in_macro_or_desugar(span) - }; - if_chain! { - if let Some(assert_span) = is_direct_expn_of(e.span, "assert"); - if !in_macro_or_desugar(assert_span) - || is_direct_expn_of(assert_span, "debug_assert") - .map_or(false, debug_assert_not_in_macro_or_desugar); - if let ExprKind::Unary(_, ref lit) = e.node; - if let Some(bool_const) = constant(cx, cx.tables, lit); - then { - match bool_const.0 { - Constant::Bool(true) => { - span_help_and_lint( - cx, - ASSERTIONS_ON_CONSTANTS, - e.span, - "`assert!(true)` will be optimized out by the compiler", - "remove it" - ); - }, - Constant::Bool(false) if !is_debug_assert => { - span_help_and_lint( - cx, - ASSERTIONS_ON_CONSTANTS, - e.span, - "`assert!(false)` should probably be replaced", - "use `panic!()` or `unreachable!()`" - ); - }, - _ => (), + if let Some(assert_span) = is_direct_expn_of(e.span, "assert") { + if in_macro_or_desugar(assert_span) { + return; + } + if let Some(debug_assert_span) = is_direct_expn_of(assert_span, "debug_assert") { + if in_macro_or_desugar(debug_assert_span) { + return; + } + is_debug_assert = true; + } + if let ExprKind::Unary(_, ref lit) = e.node { + if let Some((bool_const, _)) = constant(cx, cx.tables, lit) { + if let Constant::Bool(is_true) bool_const { + if is_true { + span_help_and_lint( + cx, + ASSERTIONS_ON_CONSTANTS, + e.span, + "`assert!(true)` will be optimized out by the compiler", + "remove it" + ); + } else if !is_debug_assert { + span_help_and_lint( + cx, + ASSERTIONS_ON_CONSTANTS, + e.span, + "`assert!(false)` should probably be replaced", + "use `panic!()` or `unreachable!()`" + ); + } + } } } } From 533bdfbdbc52c87d03959a4d58be6301f157da6e Mon Sep 17 00:00:00 2001 From: Lzu Tao Date: Sun, 18 Aug 2019 01:46:44 +0700 Subject: [PATCH 8/9] Fix assertions_on_constants lint --- clippy_lints/src/assertions_on_constants.rs | 63 ++++++++++----------- 1 file changed, 30 insertions(+), 33 deletions(-) diff --git a/clippy_lints/src/assertions_on_constants.rs b/clippy_lints/src/assertions_on_constants.rs index 22dbd3ecc..32ca1cc1b 100644 --- a/clippy_lints/src/assertions_on_constants.rs +++ b/clippy_lints/src/assertions_on_constants.rs @@ -1,11 +1,9 @@ -use if_chain::if_chain; use rustc::hir::{Expr, ExprKind}; use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass}; use rustc::{declare_lint_pass, declare_tool_lint}; -use syntax_pos::Span; use crate::consts::{constant, Constant}; -use crate::utils::{in_macro_or_desugar, is_direct_expn_of, span_help_and_lint}; +use crate::utils::{in_macro_or_desugar, is_direct_expn_of, is_expn_of, span_help_and_lint}; declare_clippy_lint! { /// **What it does:** Checks for `assert!(true)` and `assert!(false)` calls. @@ -33,40 +31,39 @@ declare_lint_pass!(AssertionsOnConstants => [ASSERTIONS_ON_CONSTANTS]); impl<'a, 'tcx> LateLintPass<'a, 'tcx> for AssertionsOnConstants { fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, e: &'tcx Expr) { - let mut is_debug_assert = false; - if let Some(assert_span) = is_direct_expn_of(e.span, "assert") { - if in_macro_or_desugar(assert_span) { - return; - } - if let Some(debug_assert_span) = is_direct_expn_of(assert_span, "debug_assert") { - if in_macro_or_desugar(debug_assert_span) { - return; - } - is_debug_assert = true; - } + let lint_assert_cb = |is_debug_assert: bool| { if let ExprKind::Unary(_, ref lit) = e.node { - if let Some((bool_const, _)) = constant(cx, cx.tables, lit) { - if let Constant::Bool(is_true) bool_const { - if is_true { - span_help_and_lint( - cx, - ASSERTIONS_ON_CONSTANTS, - e.span, - "`assert!(true)` will be optimized out by the compiler", - "remove it" - ); - } else if !is_debug_assert { - span_help_and_lint( - cx, - ASSERTIONS_ON_CONSTANTS, - e.span, - "`assert!(false)` should probably be replaced", - "use `panic!()` or `unreachable!()`" - ); - } + if let Some((Constant::Bool(is_true), _)) = constant(cx, cx.tables, lit) { + if is_true { + span_help_and_lint( + cx, + ASSERTIONS_ON_CONSTANTS, + e.span, + "`assert!(true)` will be optimized out by the compiler", + "remove it", + ); + } else if !is_debug_assert { + span_help_and_lint( + cx, + ASSERTIONS_ON_CONSTANTS, + e.span, + "`assert!(false)` should probably be replaced", + "use `panic!()` or `unreachable!()`", + ); } } } + }; + if let Some(debug_assert_span) = is_expn_of(e.span, "debug_assert") { + if in_macro_or_desugar(debug_assert_span) { + return; + } + lint_assert_cb(true); + } else if let Some(assert_span) = is_direct_expn_of(e.span, "assert") { + if in_macro_or_desugar(assert_span) { + return; + } + lint_assert_cb(false); } } } From b313d25350bf8df41e65485a65b4536fe267bec0 Mon Sep 17 00:00:00 2001 From: Michael Wright Date: Sun, 18 Aug 2019 13:14:47 +0200 Subject: [PATCH 9/9] Fix breakage due to rust-lang/rust#61708 --- clippy_lints/src/copies.rs | 10 +++++----- clippy_lints/src/utils/author.rs | 6 ++++++ clippy_lints/src/utils/inspector.rs | 6 ++++++ clippy_lints/src/utils/mod.rs | 2 +- 4 files changed, 18 insertions(+), 6 deletions(-) diff --git a/clippy_lints/src/copies.rs b/clippy_lints/src/copies.rs index 64239bb4d..01989625b 100644 --- a/clippy_lints/src/copies.rs +++ b/clippy_lints/src/copies.rs @@ -298,16 +298,16 @@ fn bindings<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, pat: &Pat) -> FxHashMap { + for pat in fields { + bindings_impl(cx, pat, map); + } + }, PatKind::Struct(_, ref fields, _) => { for pat in fields { bindings_impl(cx, &pat.pat, map); } }, - PatKind::Tuple(ref fields, _) => { - for pat in fields { - bindings_impl(cx, pat, map); - } - }, PatKind::Slice(ref lhs, ref mid, ref rhs) => { for pat in lhs { bindings_impl(cx, pat, map); diff --git a/clippy_lints/src/utils/author.rs b/clippy_lints/src/utils/author.rs index 6d9eddac8..4cf52ddac 100644 --- a/clippy_lints/src/utils/author.rs +++ b/clippy_lints/src/utils/author.rs @@ -549,6 +549,12 @@ impl<'tcx> Visitor<'tcx> for PrintVisitor { println!(" if {}.len() == {};", fields_pat, fields.len()); println!(" // unimplemented: field checks"); }, + PatKind::Or(ref fields) => { + let fields_pat = self.next("fields"); + println!("Or(ref {}) = {};", fields_pat, current); + println!(" if {}.len() == {};", fields_pat, fields.len()); + println!(" // unimplemented: field checks"); + }, PatKind::TupleStruct(ref path, ref fields, skip_pos) => { let path_pat = self.next("path"); let fields_pat = self.next("fields"); diff --git a/clippy_lints/src/utils/inspector.rs b/clippy_lints/src/utils/inspector.rs index 309b5165c..b48ef7d29 100644 --- a/clippy_lints/src/utils/inspector.rs +++ b/clippy_lints/src/utils/inspector.rs @@ -410,6 +410,12 @@ fn print_pat(cx: &LateContext<'_, '_>, pat: &hir::Pat, indent: usize) { print_pat(cx, inner, indent + 1); } }, + hir::PatKind::Or(ref fields) => { + println!("{}Or", ind); + for field in fields { + print_pat(cx, field, indent + 1); + } + }, hir::PatKind::Struct(ref path, ref fields, ignore) => { println!("{}Struct", ind); println!( diff --git a/clippy_lints/src/utils/mod.rs b/clippy_lints/src/utils/mod.rs index a25f25041..020068d46 100644 --- a/clippy_lints/src/utils/mod.rs +++ b/clippy_lints/src/utils/mod.rs @@ -791,7 +791,7 @@ pub fn is_refutable(cx: &LateContext<'_, '_>, pat: &Pat) -> bool { PatKind::Box(ref pat) | PatKind::Ref(ref pat, _) => is_refutable(cx, pat), PatKind::Lit(..) | PatKind::Range(..) => true, PatKind::Path(ref qpath) => is_enum_variant(cx, qpath, pat.hir_id), - PatKind::Tuple(ref pats, _) => are_refutable(cx, pats.iter().map(|pat| &**pat)), + PatKind::Or(ref pats) | PatKind::Tuple(ref pats, _) => are_refutable(cx, pats.iter().map(|pat| &**pat)), PatKind::Struct(ref qpath, ref fields, _) => { if is_enum_variant(cx, qpath, pat.hir_id) { true