Skip to content

Commit

Permalink
Merge branch 'origin/master' into suspicious_map
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremystucki committed Aug 18, 2019
2 parents 72e4e4a + e92c489 commit 5df84f2
Show file tree
Hide file tree
Showing 26 changed files with 153 additions and 174 deletions.
14 changes: 0 additions & 14 deletions PUBLISH.md

This file was deleted.

45 changes: 20 additions & 25 deletions clippy_lints/src/assertions_on_constants.rs
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -33,42 +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;
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) => {
let lint_assert_cb = |is_debug_assert: bool| {
if let ExprKind::Unary(_, ref lit) = e.node {
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"
"remove it",
);
},
Constant::Bool(false) if !is_debug_assert => {
} 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!()`"
"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);
}
}
}
8 changes: 4 additions & 4 deletions clippy_lints/src/copies.rs
Original file line number Diff line number Diff line change
Expand Up @@ -298,14 +298,14 @@ fn bindings<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, pat: &Pat) -> FxHashMap<LocalI
bindings_impl(cx, as_pat, map);
}
},
PatKind::Struct(_, ref fields, _) => {
PatKind::Or(ref fields) | PatKind::Tuple(ref fields, _) => {
for pat in fields {
bindings_impl(cx, &pat.node.pat, map);
bindings_impl(cx, pat, map);
}
},
PatKind::Tuple(ref fields, _) => {
PatKind::Struct(_, ref fields, _) => {
for pat in fields {
bindings_impl(cx, pat, map);
bindings_impl(cx, &pat.pat, map);
}
},
PatKind::Slice(ref lhs, ref mid, ref rhs) => {
Expand Down
4 changes: 2 additions & 2 deletions clippy_lints/src/dbg_macro.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions clippy_lints/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -682,7 +682,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![
Expand Down
4 changes: 2 additions & 2 deletions clippy_lints/src/matches.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand Down Expand Up @@ -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! {
Expand Down
10 changes: 7 additions & 3 deletions clippy_lints/src/misc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
6 changes: 3 additions & 3 deletions clippy_lints/src/misc_early.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand All @@ -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) {
Expand All @@ -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(
Expand Down
4 changes: 2 additions & 2 deletions clippy_lints/src/non_expressive_names.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
},
Expand Down
6 changes: 4 additions & 2 deletions clippy_lints/src/panic_unimplemented.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
12 changes: 8 additions & 4 deletions clippy_lints/src/ranges.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
6 changes: 5 additions & 1 deletion clippy_lints/src/returns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,11 @@ fn attr_is_cfg(attr: &ast::Attribute) -> bool {

// get the def site
fn get_def(span: Span) -> Option<Span> {
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?
Expand Down
8 changes: 4 additions & 4 deletions clippy_lints/src/shadow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
},
Expand Down
6 changes: 6 additions & 0 deletions clippy_lints/src/utils/author.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
12 changes: 9 additions & 3 deletions clippy_lints/src/utils/inspector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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!(
Expand All @@ -420,11 +426,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) => {
Expand Down
Loading

0 comments on commit 5df84f2

Please sign in to comment.