mirror of
https://github.com/rust-lang/rust.git
synced 2024-11-23 15:23:46 +00:00
Auto merge of #3597 - xfix:match-ergonomics, r=phansch
Match ergonomics (lints from A to B)
This commit is contained in:
commit
aee138a7cf
@ -73,7 +73,7 @@ impl LintPass for Pass {
|
||||
|
||||
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass {
|
||||
fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, e: &'tcx Expr) {
|
||||
if let ExprKind::Lit(ref lit) = e.node {
|
||||
if let ExprKind::Lit(lit) = &e.node {
|
||||
check_lit(cx, lit, e);
|
||||
}
|
||||
}
|
||||
|
@ -73,8 +73,8 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Arithmetic {
|
||||
return;
|
||||
}
|
||||
}
|
||||
match expr.node {
|
||||
hir::ExprKind::Binary(ref op, ref l, ref r) => {
|
||||
match &expr.node {
|
||||
hir::ExprKind::Binary(op, l, r) => {
|
||||
match op.node {
|
||||
hir::BinOpKind::And
|
||||
| hir::BinOpKind::Or
|
||||
@ -100,7 +100,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Arithmetic {
|
||||
self.expr_span = Some(expr.span);
|
||||
}
|
||||
},
|
||||
hir::ExprKind::Unary(hir::UnOp::UnNeg, ref arg) => {
|
||||
hir::ExprKind::Unary(hir::UnOp::UnNeg, arg) => {
|
||||
let ty = cx.tables.expr_ty(arg);
|
||||
if ty.is_integral() {
|
||||
span_lint(cx, INTEGER_ARITHMETIC, expr.span, "integer arithmetic detected");
|
||||
|
@ -70,9 +70,9 @@ impl LintPass for AssignOps {
|
||||
|
||||
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for AssignOps {
|
||||
fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx hir::Expr) {
|
||||
match expr.node {
|
||||
hir::ExprKind::AssignOp(op, ref lhs, ref rhs) => {
|
||||
if let hir::ExprKind::Binary(binop, ref l, ref r) = rhs.node {
|
||||
match &expr.node {
|
||||
hir::ExprKind::AssignOp(op, lhs, rhs) => {
|
||||
if let hir::ExprKind::Binary(binop, l, r) = &rhs.node {
|
||||
if op.node == binop.node {
|
||||
let lint = |assignee: &hir::Expr, rhs_other: &hir::Expr| {
|
||||
span_lint_and_then(
|
||||
@ -122,8 +122,8 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for AssignOps {
|
||||
}
|
||||
}
|
||||
},
|
||||
hir::ExprKind::Assign(ref assignee, ref e) => {
|
||||
if let hir::ExprKind::Binary(op, ref l, ref r) = e.node {
|
||||
hir::ExprKind::Assign(assignee, e) => {
|
||||
if let hir::ExprKind::Binary(op, l, r) = &e.node {
|
||||
#[allow(clippy::cyclomatic_complexity)]
|
||||
let lint = |assignee: &hir::Expr, rhs: &hir::Expr| {
|
||||
let ty = cx.tables.expr_ty(assignee);
|
||||
@ -150,8 +150,8 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for AssignOps {
|
||||
if_chain! {
|
||||
if parent_impl != ast::CRATE_NODE_ID;
|
||||
if let hir::Node::Item(item) = cx.tcx.hir().get(parent_impl);
|
||||
if let hir::ItemKind::Impl(_, _, _, _, Some(ref trait_ref), _, _) =
|
||||
item.node;
|
||||
if let hir::ItemKind::Impl(_, _, _, _, Some(trait_ref), _, _) =
|
||||
&item.node;
|
||||
if trait_ref.path.def.def_id() == trait_id;
|
||||
then { return; }
|
||||
}
|
||||
|
@ -212,7 +212,7 @@ impl LintPass for AttrPass {
|
||||
|
||||
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for AttrPass {
|
||||
fn check_attribute(&mut self, cx: &LateContext<'a, 'tcx>, attr: &'tcx Attribute) {
|
||||
if let Some(ref items) = attr.meta_item_list() {
|
||||
if let Some(items) = &attr.meta_item_list() {
|
||||
match &*attr.name().as_str() {
|
||||
"allow" | "warn" | "deny" | "forbid" => {
|
||||
check_clippy_lint_names(cx, items);
|
||||
@ -224,8 +224,8 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for AttrPass {
|
||||
}
|
||||
for item in items {
|
||||
if_chain! {
|
||||
if let NestedMetaItemKind::MetaItem(ref mi) = item.node;
|
||||
if let MetaItemKind::NameValue(ref lit) = mi.node;
|
||||
if let NestedMetaItemKind::MetaItem(mi) = &item.node;
|
||||
if let MetaItemKind::NameValue(lit) = &mi.node;
|
||||
if mi.name() == "since";
|
||||
then {
|
||||
check_semver(cx, item.span, lit);
|
||||
@ -244,7 +244,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for AttrPass {
|
||||
let skip_unused_imports = item.attrs.iter().any(|attr| attr.name() == "macro_use");
|
||||
|
||||
for attr in &item.attrs {
|
||||
if let Some(ref lint_list) = attr.meta_item_list() {
|
||||
if let Some(lint_list) = &attr.meta_item_list() {
|
||||
match &*attr.name().as_str() {
|
||||
"allow" | "warn" | "deny" | "forbid" => {
|
||||
// whitelist `unused_imports` and `deprecated` for `use` items
|
||||
@ -381,9 +381,9 @@ fn is_relevant_trait(tcx: TyCtxt<'_, '_, '_>, item: &TraitItem) -> bool {
|
||||
|
||||
fn is_relevant_block(tcx: TyCtxt<'_, '_, '_>, tables: &ty::TypeckTables<'_>, block: &Block) -> bool {
|
||||
if let Some(stmt) = block.stmts.first() {
|
||||
match stmt.node {
|
||||
match &stmt.node {
|
||||
StmtKind::Decl(_, _) => true,
|
||||
StmtKind::Expr(ref expr, _) | StmtKind::Semi(ref expr, _) => is_relevant_expr(tcx, tables, expr),
|
||||
StmtKind::Expr(expr, _) | StmtKind::Semi(expr, _) => is_relevant_expr(tcx, tables, expr),
|
||||
}
|
||||
} else {
|
||||
block.expr.as_ref().map_or(false, |e| is_relevant_expr(tcx, tables, e))
|
||||
@ -391,12 +391,12 @@ fn is_relevant_block(tcx: TyCtxt<'_, '_, '_>, tables: &ty::TypeckTables<'_>, blo
|
||||
}
|
||||
|
||||
fn is_relevant_expr(tcx: TyCtxt<'_, '_, '_>, tables: &ty::TypeckTables<'_>, expr: &Expr) -> bool {
|
||||
match expr.node {
|
||||
ExprKind::Block(ref block, _) => is_relevant_block(tcx, tables, block),
|
||||
ExprKind::Ret(Some(ref e)) => is_relevant_expr(tcx, tables, e),
|
||||
match &expr.node {
|
||||
ExprKind::Block(block, _) => is_relevant_block(tcx, tables, block),
|
||||
ExprKind::Ret(Some(e)) => is_relevant_expr(tcx, tables, e),
|
||||
ExprKind::Ret(None) | ExprKind::Break(_, None) => false,
|
||||
ExprKind::Call(ref path_expr, _) => {
|
||||
if let ExprKind::Path(ref qpath) = path_expr.node {
|
||||
ExprKind::Call(path_expr, _) => {
|
||||
if let ExprKind::Path(qpath) = &path_expr.node {
|
||||
if let Some(fun_id) = opt_def_id(tables.qpath_def(qpath, path_expr.hir_id)) {
|
||||
!match_def_path(tcx, fun_id, &paths::BEGIN_PANIC)
|
||||
} else {
|
||||
@ -443,7 +443,7 @@ fn check_attrs(cx: &LateContext<'_, '_>, span: Span, name: Name, attrs: &[Attrib
|
||||
}
|
||||
}
|
||||
|
||||
if let Some(ref values) = attr.meta_item_list() {
|
||||
if let Some(values) = attr.meta_item_list() {
|
||||
if values.len() != 1 || attr.name() != "inline" {
|
||||
continue;
|
||||
}
|
||||
@ -463,7 +463,7 @@ fn check_attrs(cx: &LateContext<'_, '_>, span: Span, name: Name, attrs: &[Attrib
|
||||
}
|
||||
|
||||
fn check_semver(cx: &LateContext<'_, '_>, span: Span, lit: &Lit) {
|
||||
if let LitKind::Str(ref is, _) = lit.node {
|
||||
if let LitKind::Str(is, _) = lit.node {
|
||||
if Version::parse(&is.as_str()).is_ok() {
|
||||
return;
|
||||
}
|
||||
@ -477,7 +477,7 @@ fn check_semver(cx: &LateContext<'_, '_>, span: Span, lit: &Lit) {
|
||||
}
|
||||
|
||||
fn is_word(nmi: &NestedMetaItem, expected: &str) -> bool {
|
||||
if let NestedMetaItemKind::MetaItem(ref mi) = nmi.node {
|
||||
if let NestedMetaItemKind::MetaItem(mi) = &nmi.node {
|
||||
mi.is_word() && mi.name() == expected
|
||||
} else {
|
||||
false
|
||||
@ -512,7 +512,7 @@ impl EarlyLintPass for CfgAttrPass {
|
||||
if_chain! {
|
||||
// check cfg_attr
|
||||
if attr.name() == "cfg_attr";
|
||||
if let Some(ref items) = attr.meta_item_list();
|
||||
if let Some(items) = attr.meta_item_list();
|
||||
if items.len() == 2;
|
||||
// check for `rustfmt`
|
||||
if let Some(feature_item) = items[0].meta_item();
|
||||
|
@ -121,7 +121,7 @@ impl LintPass for BitMask {
|
||||
|
||||
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for BitMask {
|
||||
fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, e: &'tcx Expr) {
|
||||
if let ExprKind::Binary(ref cmp, ref left, ref right) = e.node {
|
||||
if let ExprKind::Binary(cmp, left, right) = &e.node {
|
||||
if cmp.node.is_comparison() {
|
||||
if let Some(cmp_opt) = fetch_int_literal(cx, right) {
|
||||
check_compare(cx, left, cmp.node, cmp_opt, e.span)
|
||||
@ -131,13 +131,13 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for BitMask {
|
||||
}
|
||||
}
|
||||
if_chain! {
|
||||
if let ExprKind::Binary(ref op, ref left, ref right) = e.node;
|
||||
if let ExprKind::Binary(op, left, right) = &e.node;
|
||||
if BinOpKind::Eq == op.node;
|
||||
if let ExprKind::Binary(ref op1, ref left1, ref right1) = left.node;
|
||||
if let ExprKind::Binary(op1, left1, right1) = &left.node;
|
||||
if BinOpKind::BitAnd == op1.node;
|
||||
if let ExprKind::Lit(ref lit) = right1.node;
|
||||
if let ExprKind::Lit(lit) = &right1.node;
|
||||
if let LitKind::Int(n, _) = lit.node;
|
||||
if let ExprKind::Lit(ref lit1) = right.node;
|
||||
if let ExprKind::Lit(lit1) = &right.node;
|
||||
if let LitKind::Int(0, _) = lit1.node;
|
||||
if n.leading_zeros() == n.count_zeros();
|
||||
if n > u128::from(self.verbose_bit_mask_threshold);
|
||||
@ -173,7 +173,7 @@ fn invert_cmp(cmp: BinOpKind) -> BinOpKind {
|
||||
}
|
||||
|
||||
fn check_compare(cx: &LateContext<'_, '_>, bit_op: &Expr, cmp_op: BinOpKind, cmp_value: u128, span: Span) {
|
||||
if let ExprKind::Binary(ref op, ref left, ref right) = bit_op.node {
|
||||
if let ExprKind::Binary(op, left, right) = &bit_op.node {
|
||||
if op.node != BinOpKind::BitAnd && op.node != BinOpKind::BitOr {
|
||||
return;
|
||||
}
|
||||
|
@ -88,11 +88,11 @@ const COMPLEX_BLOCK_MESSAGE: &str = "in an 'if' condition, avoid complex blocks
|
||||
|
||||
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for BlockInIfCondition {
|
||||
fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr) {
|
||||
if let ExprKind::If(ref check, ref then, _) = expr.node {
|
||||
if let ExprKind::Block(ref block, _) = check.node {
|
||||
if let ExprKind::If(check, then, _) = &expr.node {
|
||||
if let ExprKind::Block(block, _) = &check.node {
|
||||
if block.rules == DefaultBlock {
|
||||
if block.stmts.is_empty() {
|
||||
if let Some(ref ex) = block.expr {
|
||||
if let Some(ex) = &block.expr {
|
||||
// don't dig into the expression here, just suggest that they remove
|
||||
// the block
|
||||
if in_macro(expr.span) || differing_macro_contexts(expr.span, ex.span) {
|
||||
|
@ -96,7 +96,7 @@ struct Hir2Qmm<'a, 'tcx: 'a, 'v> {
|
||||
impl<'a, 'tcx, 'v> Hir2Qmm<'a, 'tcx, 'v> {
|
||||
fn extract(&mut self, op: BinOpKind, a: &[&'v Expr], mut v: Vec<Bool>) -> Result<Vec<Bool>, String> {
|
||||
for a in a {
|
||||
if let ExprKind::Binary(binop, ref lhs, ref rhs) = a.node {
|
||||
if let ExprKind::Binary(binop, lhs, rhs) = &a.node {
|
||||
if binop.node == op {
|
||||
v = self.extract(op, &[lhs, rhs], v)?;
|
||||
continue;
|
||||
@ -110,14 +110,14 @@ impl<'a, 'tcx, 'v> Hir2Qmm<'a, 'tcx, 'v> {
|
||||
fn run(&mut self, e: &'v Expr) -> Result<Bool, String> {
|
||||
// prevent folding of `cfg!` macros and the like
|
||||
if !in_macro(e.span) {
|
||||
match e.node {
|
||||
ExprKind::Unary(UnNot, ref inner) => return Ok(Bool::Not(box self.run(inner)?)),
|
||||
ExprKind::Binary(binop, ref lhs, ref rhs) => match binop.node {
|
||||
match &e.node {
|
||||
ExprKind::Unary(UnNot, inner) => return Ok(Bool::Not(box self.run(inner)?)),
|
||||
ExprKind::Binary(binop, lhs, rhs) => match &binop.node {
|
||||
BinOpKind::Or => return Ok(Bool::Or(self.extract(BinOpKind::Or, &[lhs, rhs], Vec::new())?)),
|
||||
BinOpKind::And => return Ok(Bool::And(self.extract(BinOpKind::And, &[lhs, rhs], Vec::new())?)),
|
||||
_ => (),
|
||||
},
|
||||
ExprKind::Lit(ref lit) => match lit.node {
|
||||
ExprKind::Lit(lit) => match lit.node {
|
||||
LitKind::Bool(true) => return Ok(Bool::True),
|
||||
LitKind::Bool(false) => return Ok(Bool::False),
|
||||
_ => (),
|
||||
@ -130,8 +130,8 @@ impl<'a, 'tcx, 'v> Hir2Qmm<'a, 'tcx, 'v> {
|
||||
#[allow(clippy::cast_possible_truncation)]
|
||||
return Ok(Bool::Term(n as u8));
|
||||
}
|
||||
let negated = match e.node {
|
||||
ExprKind::Binary(binop, ref lhs, ref rhs) => {
|
||||
let negated = match &e.node {
|
||||
ExprKind::Binary(binop, lhs, rhs) => {
|
||||
if !implements_ord(self.cx, lhs) {
|
||||
continue;
|
||||
}
|
||||
@ -184,8 +184,8 @@ impl<'a, 'tcx, 'v> SuggestContext<'a, 'tcx, 'v> {
|
||||
}
|
||||
|
||||
fn simplify_not(&self, expr: &Expr) -> Option<String> {
|
||||
match expr.node {
|
||||
ExprKind::Binary(binop, ref lhs, ref rhs) => {
|
||||
match &expr.node {
|
||||
ExprKind::Binary(binop, lhs, rhs) => {
|
||||
if !implements_ord(self.cx, lhs) {
|
||||
return None;
|
||||
}
|
||||
@ -201,7 +201,7 @@ impl<'a, 'tcx, 'v> SuggestContext<'a, 'tcx, 'v> {
|
||||
}
|
||||
.and_then(|op| Some(format!("{}{}{}", self.snip(lhs)?, op, self.snip(rhs)?)))
|
||||
},
|
||||
ExprKind::MethodCall(ref path, _, ref args) if args.len() == 1 => {
|
||||
ExprKind::MethodCall(path, _, args) if args.len() == 1 => {
|
||||
let type_of_receiver = self.cx.tables.expr_ty(&args[0]);
|
||||
if !match_type(self.cx, type_of_receiver, &paths::OPTION)
|
||||
&& !match_type(self.cx, type_of_receiver, &paths::RESULT)
|
||||
@ -221,14 +221,14 @@ impl<'a, 'tcx, 'v> SuggestContext<'a, 'tcx, 'v> {
|
||||
|
||||
fn recurse(&mut self, suggestion: &Bool) -> Option<()> {
|
||||
use quine_mc_cluskey::Bool::*;
|
||||
match *suggestion {
|
||||
match suggestion {
|
||||
True => {
|
||||
self.output.push_str("true");
|
||||
},
|
||||
False => {
|
||||
self.output.push_str("false");
|
||||
},
|
||||
Not(ref inner) => match **inner {
|
||||
Not(inner) => match **inner {
|
||||
And(_) | Or(_) => {
|
||||
self.output.push('!');
|
||||
self.output.push('(');
|
||||
@ -251,7 +251,7 @@ impl<'a, 'tcx, 'v> SuggestContext<'a, 'tcx, 'v> {
|
||||
self.recurse(inner)?;
|
||||
},
|
||||
},
|
||||
And(ref v) => {
|
||||
And(v) => {
|
||||
for (index, inner) in v.iter().enumerate() {
|
||||
if index > 0 {
|
||||
self.output.push_str(" && ");
|
||||
@ -265,7 +265,7 @@ impl<'a, 'tcx, 'v> SuggestContext<'a, 'tcx, 'v> {
|
||||
}
|
||||
}
|
||||
},
|
||||
Or(ref v) => {
|
||||
Or(v) => {
|
||||
for (index, inner) in v.iter().enumerate() {
|
||||
if index > 0 {
|
||||
self.output.push_str(" || ");
|
||||
@ -273,7 +273,7 @@ impl<'a, 'tcx, 'v> SuggestContext<'a, 'tcx, 'v> {
|
||||
self.recurse(inner);
|
||||
}
|
||||
},
|
||||
Term(n) => {
|
||||
&Term(n) => {
|
||||
let snip = self.snip(self.terminals[n as usize])?;
|
||||
self.output.push_str(&snip);
|
||||
},
|
||||
@ -325,22 +325,22 @@ struct Stats {
|
||||
|
||||
fn terminal_stats(b: &Bool) -> Stats {
|
||||
fn recurse(b: &Bool, stats: &mut Stats) {
|
||||
match *b {
|
||||
match b {
|
||||
True | False => stats.ops += 1,
|
||||
Not(ref inner) => {
|
||||
Not(inner) => {
|
||||
match **inner {
|
||||
And(_) | Or(_) => stats.ops += 1, // brackets are also operations
|
||||
_ => stats.negations += 1,
|
||||
}
|
||||
recurse(inner, stats);
|
||||
},
|
||||
And(ref v) | Or(ref v) => {
|
||||
And(v) | Or(v) => {
|
||||
stats.ops += v.len() - 1;
|
||||
for inner in v {
|
||||
recurse(inner, stats);
|
||||
}
|
||||
},
|
||||
Term(n) => stats.terminals[n as usize] += 1,
|
||||
&Term(n) => stats.terminals[n as usize] += 1,
|
||||
}
|
||||
}
|
||||
use quine_mc_cluskey::Bool::*;
|
||||
@ -461,11 +461,11 @@ impl<'a, 'tcx> Visitor<'tcx> for NonminimalBoolVisitor<'a, 'tcx> {
|
||||
if in_macro(e.span) {
|
||||
return;
|
||||
}
|
||||
match e.node {
|
||||
match &e.node {
|
||||
ExprKind::Binary(binop, _, _) if binop.node == BinOpKind::Or || binop.node == BinOpKind::And => {
|
||||
self.bool_expr(e)
|
||||
},
|
||||
ExprKind::Unary(UnNot, ref inner) => {
|
||||
ExprKind::Unary(UnNot, inner) => {
|
||||
if self.cx.tables.node_types()[inner.hir_id].is_bool() {
|
||||
self.bool_expr(e);
|
||||
} else {
|
||||
|
Loading…
Reference in New Issue
Block a user