Skip to content

Commit

Permalink
Auto merge of #4948 - lzutao:rustup-67538, r=phansch
Browse files Browse the repository at this point in the history
rustup "Add span information to `ExprKind::Assign`"

cc rust-lang/rust#67538
changelog: none
  • Loading branch information
bors committed Dec 24, 2019
2 parents 82b0325 + 652666b commit 74bb5e0
Show file tree
Hide file tree
Showing 19 changed files with 45 additions and 34 deletions.
2 changes: 1 addition & 1 deletion clippy_lints/src/assign_ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for AssignOps {
}
}
},
hir::ExprKind::Assign(assignee, e) => {
hir::ExprKind::Assign(assignee, e, _) => {
if let hir::ExprKind::Binary(op, l, r) = &e.kind {
#[allow(clippy::cognitive_complexity)]
let lint = |assignee: &hir::Expr, rhs: &hir::Expr| {
Expand Down
6 changes: 3 additions & 3 deletions clippy_lints/src/eval_order_dependence.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for EvalOrderDependence {
fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr) {
// Find a write to a local variable.
match expr.kind {
ExprKind::Assign(ref lhs, _) | ExprKind::AssignOp(_, ref lhs, _) => {
ExprKind::Assign(ref lhs, ..) | ExprKind::AssignOp(_, ref lhs, _) => {
if let ExprKind::Path(ref qpath) = lhs.kind {
if let QPath::Resolved(_, ref path) = *qpath {
if path.segments.len() == 1 {
Expand Down Expand Up @@ -224,7 +224,7 @@ fn check_expr<'a, 'tcx>(vis: &mut ReadVisitor<'a, 'tcx>, expr: &'tcx Expr) -> St
| ExprKind::Tup(_)
| ExprKind::MethodCall(..)
| ExprKind::Call(_, _)
| ExprKind::Assign(_, _)
| ExprKind::Assign(..)
| ExprKind::Index(_, _)
| ExprKind::Repeat(_, _)
| ExprKind::Struct(_, _, _) => {
Expand Down Expand Up @@ -345,7 +345,7 @@ impl<'a, 'tcx> Visitor<'tcx> for ReadVisitor<'a, 'tcx> {
/// Returns `true` if `expr` is the LHS of an assignment, like `expr = ...`.
fn is_in_assignment_position(cx: &LateContext<'_, '_>, expr: &Expr) -> bool {
if let Some(parent) = get_parent_expr(cx, expr) {
if let ExprKind::Assign(ref lhs, _) = parent.kind {
if let ExprKind::Assign(ref lhs, ..) = parent.kind {
return lhs.hir_id == expr.hir_id;
}
}
Expand Down
2 changes: 1 addition & 1 deletion clippy_lints/src/formatting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ impl EarlyLintPass for Formatting {

/// Implementation of the `SUSPICIOUS_ASSIGNMENT_FORMATTING` lint.
fn check_assign(cx: &EarlyContext<'_>, expr: &Expr) {
if let ExprKind::Assign(ref lhs, ref rhs) = expr.kind {
if let ExprKind::Assign(ref lhs, ref rhs, _) = expr.kind {
if !differing_macro_contexts(lhs.span, rhs.span) && !lhs.span.from_expansion() {
let eq_span = lhs.span.between(rhs.span);
if let ExprKind::Unary(op, ref sub_rhs) = rhs.kind {
Expand Down
2 changes: 1 addition & 1 deletion clippy_lints/src/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -615,7 +615,7 @@ impl<'a, 'tcx> intravisit::Visitor<'tcx> for StaticMutVisitor<'a, 'tcx> {
tys.clear();
}
},
Assign(ref target, _) | AssignOp(_, ref target, _) | AddrOf(_, hir::Mutability::Mut, ref target) => {
Assign(ref target, ..) | AssignOp(_, ref target, _) | AddrOf(_, hir::Mutability::Mut, ref target) => {
self.mutates_static |= is_mutated_static(self.cx, target)
},
_ => {},
Expand Down
2 changes: 1 addition & 1 deletion clippy_lints/src/let_if_seq.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ fn check_assign<'a, 'tcx>(
if block.expr.is_none();
if let Some(expr) = block.stmts.iter().last();
if let hir::StmtKind::Semi(ref expr) = expr.kind;
if let hir::ExprKind::Assign(ref var, ref value) = expr.kind;
if let hir::ExprKind::Assign(ref var, ref value, _) = expr.kind;
if let hir::ExprKind::Path(ref qpath) = var.kind;
if let Res::Local(local_id) = qpath_res(cx, qpath, var.hir_id);
if decl == local_id;
Expand Down
12 changes: 6 additions & 6 deletions clippy_lints/src/loops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -682,7 +682,7 @@ fn never_loop_expr(expr: &Expr, main_loop_id: HirId) -> NeverLoopResult {
},
ExprKind::Call(ref e, ref es) => never_loop_expr_all(&mut once(&**e).chain(es.iter()), main_loop_id),
ExprKind::Binary(_, ref e1, ref e2)
| ExprKind::Assign(ref e1, ref e2)
| ExprKind::Assign(ref e1, ref e2, _)
| ExprKind::AssignOp(_, ref e1, ref e2)
| ExprKind::Index(ref e1, ref e2) => never_loop_expr_all(&mut [&**e1, &**e2].iter().cloned(), main_loop_id),
ExprKind::Loop(ref b, _, _) => {
Expand Down Expand Up @@ -887,7 +887,7 @@ fn get_indexed_assignments<'a, 'tcx>(
e: &Expr,
var: HirId,
) -> Option<(FixedOffsetVar, FixedOffsetVar)> {
if let ExprKind::Assign(ref lhs, ref rhs) = e.kind {
if let ExprKind::Assign(ref lhs, ref rhs, _) = e.kind {
match (
get_fixed_offset_var(cx, lhs, var),
fetch_cloned_fixed_offset_var(cx, rhs, var),
Expand Down Expand Up @@ -1861,7 +1861,7 @@ impl<'a, 'tcx> Visitor<'tcx> for VarVisitor<'a, 'tcx> {

let old = self.prefer_mutable;
match expr.kind {
ExprKind::AssignOp(_, ref lhs, ref rhs) | ExprKind::Assign(ref lhs, ref rhs) => {
ExprKind::AssignOp(_, ref lhs, ref rhs) | ExprKind::Assign(ref lhs, ref rhs, _) => {
self.prefer_mutable = true;
self.visit_expr(lhs);
self.prefer_mutable = false;
Expand Down Expand Up @@ -2083,7 +2083,7 @@ impl<'a, 'tcx> Visitor<'tcx> for IncrementVisitor<'a, 'tcx> {
}
}
},
ExprKind::Assign(ref lhs, _) if lhs.hir_id == expr.hir_id => *state = VarState::DontWarn,
ExprKind::Assign(ref lhs, _, _) if lhs.hir_id == expr.hir_id => *state = VarState::DontWarn,
ExprKind::AddrOf(BorrowKind::Ref, mutability, _) if mutability == Mutability::Mut => {
*state = VarState::DontWarn
},
Expand Down Expand Up @@ -2161,7 +2161,7 @@ impl<'a, 'tcx> Visitor<'tcx> for InitializeVisitor<'a, 'tcx> {
ExprKind::AssignOp(_, ref lhs, _) if lhs.hir_id == expr.hir_id => {
self.state = VarState::DontWarn;
},
ExprKind::Assign(ref lhs, ref rhs) if lhs.hir_id == expr.hir_id => {
ExprKind::Assign(ref lhs, ref rhs, _) if lhs.hir_id == expr.hir_id => {
self.state = if is_integer_const(&self.cx, rhs, 0) && self.depth == 0 {
VarState::Warn
} else {
Expand Down Expand Up @@ -2303,7 +2303,7 @@ impl<'tcx> Visitor<'tcx> for LoopNestVisitor {
return;
}
match expr.kind {
ExprKind::Assign(ref path, _) | ExprKind::AssignOp(_, ref path, _) => {
ExprKind::Assign(ref path, _, _) | ExprKind::AssignOp(_, ref path, _) => {
if match_var(path, self.iterator) {
self.nesting = RuledOut;
}
Expand Down
4 changes: 3 additions & 1 deletion clippy_lints/src/misc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -590,7 +590,9 @@ fn check_to_owned(cx: &LateContext<'_, '_>, expr: &Expr, other: &Expr) {
fn is_used(cx: &LateContext<'_, '_>, expr: &Expr) -> bool {
if let Some(parent) = get_parent_expr(cx, expr) {
match parent.kind {
ExprKind::Assign(_, ref rhs) | ExprKind::AssignOp(_, _, ref rhs) => SpanlessEq::new(cx).eq_expr(rhs, expr),
ExprKind::Assign(_, ref rhs, _) | ExprKind::AssignOp(_, _, ref rhs) => {
SpanlessEq::new(cx).eq_expr(rhs, expr)
},
_ => is_used(cx, parent),
}
} else {
Expand Down
2 changes: 1 addition & 1 deletion clippy_lints/src/misc_early.rs
Original file line number Diff line number Diff line change
Expand Up @@ -451,7 +451,7 @@ impl EarlyLintPass for MiscEarlyLints {
if let ExprKind::Closure(..) = t.kind;
if let PatKind::Ident(_, ident, _) = local.pat.kind;
if let StmtKind::Semi(ref second) = w[1].kind;
if let ExprKind::Assign(_, ref call) = second.kind;
if let ExprKind::Assign(_, ref call, _) = second.kind;
if let ExprKind::Call(ref closure, _) = call.kind;
if let ExprKind::Path(_, ref path) = closure.kind;
then {
Expand Down
2 changes: 1 addition & 1 deletion clippy_lints/src/slow_vector_initialization.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for SlowVectorInit {
fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr) {
// Matches initialization on reassignements. For example: `vec = Vec::with_capacity(100)`
if_chain! {
if let ExprKind::Assign(ref left, ref right) = expr.kind;
if let ExprKind::Assign(ref left, ref right, _) = expr.kind;

// Extract variable name
if let ExprKind::Path(QPath::Resolved(_, ref path)) = left.kind;
Expand Down
4 changes: 2 additions & 2 deletions clippy_lints/src/strings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for StringAdd {
if !is_allowed(cx, STRING_ADD_ASSIGN, e.hir_id) {
let parent = get_parent_expr(cx, e);
if let Some(p) = parent {
if let ExprKind::Assign(ref target, _) = p.kind {
if let ExprKind::Assign(ref target, _, _) = p.kind {
// avoid duplicate matches
if SpanlessEq::new(cx).eq_expr(target, left) {
return;
Expand All @@ -111,7 +111,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for StringAdd {
"you added something to a string. Consider using `String::push_str()` instead",
);
}
} else if let ExprKind::Assign(ref target, ref src) = e.kind {
} else if let ExprKind::Assign(ref target, ref src, _) = e.kind {
if is_string(cx, target) && is_add(cx, src, target) {
span_lint(
cx,
Expand Down
8 changes: 4 additions & 4 deletions clippy_lints/src/swap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,11 @@ fn check_manual_swap(cx: &LateContext<'_, '_>, block: &Block) {

// foo() = bar();
if let StmtKind::Semi(ref first) = w[1].kind;
if let ExprKind::Assign(ref lhs1, ref rhs1) = first.kind;
if let ExprKind::Assign(ref lhs1, ref rhs1, _) = first.kind;

// bar() = t;
if let StmtKind::Semi(ref second) = w[2].kind;
if let ExprKind::Assign(ref lhs2, ref rhs2) = second.kind;
if let ExprKind::Assign(ref lhs2, ref rhs2, _) = second.kind;
if let ExprKind::Path(QPath::Resolved(None, ref rhs2)) = rhs2.kind;
if rhs2.segments.len() == 1;

Expand Down Expand Up @@ -222,8 +222,8 @@ fn check_suspicious_swap(cx: &LateContext<'_, '_>, block: &Block) {
if let StmtKind::Semi(ref first) = w[0].kind;
if let StmtKind::Semi(ref second) = w[1].kind;
if !differing_macro_contexts(first.span, second.span);
if let ExprKind::Assign(ref lhs0, ref rhs0) = first.kind;
if let ExprKind::Assign(ref lhs1, ref rhs1) = second.kind;
if let ExprKind::Assign(ref lhs0, ref rhs0, _) = first.kind;
if let ExprKind::Assign(ref lhs1, ref rhs1, _) = second.kind;
if SpanlessEq::new(cx).ignore_fn().eq_expr(lhs0, rhs1);
if SpanlessEq::new(cx).ignore_fn().eq_expr(lhs1, rhs0);
then {
Expand Down
2 changes: 1 addition & 1 deletion clippy_lints/src/temporary_assignment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ declare_lint_pass!(TemporaryAssignment => [TEMPORARY_ASSIGNMENT]);

impl<'a, 'tcx> LateLintPass<'a, 'tcx> for TemporaryAssignment {
fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr) {
if let ExprKind::Assign(target, _) = &expr.kind {
if let ExprKind::Assign(target, ..) = &expr.kind {
let mut base = target;
while let ExprKind::Field(f, _) | ExprKind::Index(f, _) = &base.kind {
base = f;
Expand Down
7 changes: 5 additions & 2 deletions clippy_lints/src/utils/author.rs
Original file line number Diff line number Diff line change
Expand Up @@ -380,10 +380,13 @@ impl<'tcx> Visitor<'tcx> for PrintVisitor {
self.current = block_pat;
self.visit_block(block);
},
ExprKind::Assign(ref target, ref value) => {
ExprKind::Assign(ref target, ref value, _) => {
let target_pat = self.next("target");
let value_pat = self.next("value");
println!("Assign(ref {}, ref {}) = {};", target_pat, value_pat, current);
println!(
"Assign(ref {}, ref {}, ref _span) = {};",
target_pat, value_pat, current
);
self.current = target_pat;
self.visit_expr(target);
self.current = value_pat;
Expand Down
4 changes: 2 additions & 2 deletions clippy_lints/src/utils/hir_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ impl<'a, 'tcx> SpanlessEq<'a, 'tcx> {
(&ExprKind::Continue(li), &ExprKind::Continue(ri)) => {
both(&li.label, &ri.label, |l, r| l.ident.as_str() == r.ident.as_str())
},
(&ExprKind::Assign(ref ll, ref lr), &ExprKind::Assign(ref rl, ref rr)) => {
(&ExprKind::Assign(ref ll, ref lr, _), &ExprKind::Assign(ref rl, ref rr, _)) => {
self.eq_expr(ll, rl) && self.eq_expr(lr, rr)
},
(&ExprKind::AssignOp(ref lo, ref ll, ref lr), &ExprKind::AssignOp(ref ro, ref rl, ref rr)) => {
Expand Down Expand Up @@ -412,7 +412,7 @@ impl<'a, 'tcx> SpanlessHash<'a, 'tcx> {
self.hash_name(i.ident.name);
}
},
ExprKind::Assign(ref l, ref r) => {
ExprKind::Assign(ref l, ref r, _) => {
self.hash_expr(l);
self.hash_expr(r);
},
Expand Down
2 changes: 1 addition & 1 deletion clippy_lints/src/utils/inspector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ fn print_expr(cx: &LateContext<'_, '_>, expr: &hir::Expr, indent: usize) {
hir::ExprKind::Block(_, _) => {
println!("{}Block", ind);
},
hir::ExprKind::Assign(ref lhs, ref rhs) => {
hir::ExprKind::Assign(ref lhs, ref rhs, _) => {
println!("{}Assign", ind);
println!("{}lhs:", ind);
print_expr(cx, lhs, indent + 1);
Expand Down
2 changes: 1 addition & 1 deletion clippy_lints/src/write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,7 @@ fn check_tts<'a>(cx: &EarlyContext<'a>, tts: &TokenStream, is_write: bool) -> (O
}
idx += 1;
},
ExprKind::Assign(lhs, rhs) => {
ExprKind::Assign(lhs, rhs, _) => {
if let ExprKind::Lit(_) = rhs.kind {
if let ExprKind::Path(_, p) = &lhs.kind {
let mut all_simple = true;
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/author/for_loop.stdout
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ if_chain! {
if let ExprKind::Path(ref path3) = inner.kind;
if match_qpath(path3, &["iter"]);
if arms1.len() == 2;
if let ExprKind::Assign(ref target, ref value) = arms1[0].body.kind;
if let ExprKind::Assign(ref target, ref value, ref _span) = arms1[0].body.kind;
if let ExprKind::Path(ref path4) = target.kind;
if match_qpath(path4, &["__next"]);
if let ExprKind::Path(ref path5) = value.kind;
Expand Down
2 changes: 0 additions & 2 deletions tests/ui/missing_const_for_fn/could_be_const.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,6 @@ fn generic<T>(t: T) -> T {
t
}

// FIXME: Depends on the `const_transmute` and `const_fn` feature gates.
// In the future Clippy should be able to suggest this as const, too.
fn sub(x: u32) -> usize {
unsafe { transmute(&x) }
}
Expand Down
12 changes: 10 additions & 2 deletions tests/ui/missing_const_for_fn/could_be_const.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,20 @@ LL | | }
| |_^

error: this could be a const_fn
--> $DIR/could_be_const.rs:65:9
--> $DIR/could_be_const.rs:44:1
|
LL | / fn sub(x: u32) -> usize {
LL | | unsafe { transmute(&x) }
LL | | }
| |_^

error: this could be a const_fn
--> $DIR/could_be_const.rs:63:9
|
LL | / pub fn b(self, a: &A) -> B {
LL | | B
LL | | }
| |_________^

error: aborting due to 7 previous errors
error: aborting due to 8 previous errors

0 comments on commit 74bb5e0

Please sign in to comment.