X-Git-Url: https://git.lizzy.rs/?a=blobdiff_plain;f=clippy_lints%2Fsrc%2Futils%2Fsugg.rs;h=20bea3cbabe64824fd6d17d6cc440c8bde426810;hb=5150277a4f765bb113e163adc7eb495dcbb57129;hp=3e694f40d1939df7f05b11db677737d44a7c2cf5;hpb=c6b87ef54a9a7ee9bd4bdf3ace3e449c0734cd8e;p=rust.git diff --git a/clippy_lints/src/utils/sugg.rs b/clippy_lints/src/utils/sugg.rs index 3e694f40d19..20bea3cbabe 100644 --- a/clippy_lints/src/utils/sugg.rs +++ b/clippy_lints/src/utils/sugg.rs @@ -2,7 +2,8 @@ #![deny(clippy::missing_docs_in_private_items)] use crate::utils::{higher, snippet, snippet_opt, snippet_with_macro_callsite}; -use matches::matches; +use rustc_ast::util::parser::AssocOp; +use rustc_ast::{ast, token}; use rustc_ast_pretty::pprust::token_kind_to_string; use rustc_errors::Applicability; use rustc_hir as hir; @@ -12,11 +13,6 @@ use std::borrow::Cow; use std::convert::TryInto; use std::fmt::Display; -use syntax::ast; -use syntax::token; -use syntax::util::parser::AssocOp; - -pub use crate::literal_representation::format_numeric_literal; /// A helper type to build suggestion correctly handling parenthesis. pub enum Sugg<'a> { @@ -43,7 +39,7 @@ fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> { #[allow(clippy::wrong_self_convention)] // ok, because of the function `as_ty` method impl<'a> Sugg<'a> { /// Prepare a suggestion from an expression. - pub fn hir_opt(cx: &LateContext<'_, '_>, expr: &hir::Expr<'_>) -> Option { + pub fn hir_opt(cx: &LateContext<'_>, expr: &hir::Expr<'_>) -> Option { snippet_opt(cx, expr.span).map(|snippet| { let snippet = Cow::Owned(snippet); Self::hir_from_snippet(cx, expr, snippet) @@ -52,7 +48,7 @@ pub fn hir_opt(cx: &LateContext<'_, '_>, expr: &hir::Expr<'_>) -> Option { /// Convenience function around `hir_opt` for suggestions with a default /// text. - pub fn hir(cx: &LateContext<'_, '_>, expr: &hir::Expr<'_>, default: &'a str) -> Self { + pub fn hir(cx: &LateContext<'_>, expr: &hir::Expr<'_>, default: &'a str) -> Self { Self::hir_opt(cx, expr).unwrap_or_else(|| Sugg::NonParen(Cow::Borrowed(default))) } @@ -64,7 +60,7 @@ pub fn hir(cx: &LateContext<'_, '_>, expr: &hir::Expr<'_>, default: &'a str) -> /// to /// `HasPlaceholders` pub fn hir_with_applicability( - cx: &LateContext<'_, '_>, + cx: &LateContext<'_>, expr: &hir::Expr<'_>, default: &'a str, applicability: &mut Applicability, @@ -81,7 +77,7 @@ pub fn hir_with_applicability( } /// Same as `hir`, but will use the pre expansion span if the `expr` was in a macro. - pub fn hir_with_macro_callsite(cx: &LateContext<'_, '_>, expr: &hir::Expr<'_>, default: &'a str) -> Self { + pub fn hir_with_macro_callsite(cx: &LateContext<'_>, expr: &hir::Expr<'_>, default: &'a str) -> Self { let snippet = snippet_with_macro_callsite(cx, expr.span, default); Self::hir_from_snippet(cx, expr, snippet) @@ -89,7 +85,7 @@ pub fn hir_with_macro_callsite(cx: &LateContext<'_, '_>, expr: &hir::Expr<'_>, d /// Generate a suggestion for an expression with the given snippet. This is used by the `hir_*` /// function variants of `Sugg`, since these use different snippet functions. - fn hir_from_snippet(cx: &LateContext<'_, '_>, expr: &hir::Expr<'_>, snippet: Cow<'a, str>) -> Self { + fn hir_from_snippet(cx: &LateContext<'_>, expr: &hir::Expr<'_>, snippet: Cow<'a, str>) -> Self { if let Some(range) = higher::range(cx, expr) { let op = match range.limits { ast::RangeLimits::HalfOpen => AssocOp::DotDot, @@ -113,6 +109,7 @@ fn hir_from_snippet(cx: &LateContext<'_, '_>, expr: &hir::Expr<'_>, snippet: Cow | hir::ExprKind::Field(..) | hir::ExprKind::Index(..) | hir::ExprKind::InlineAsm(..) + | hir::ExprKind::LlvmInlineAsm(..) | hir::ExprKind::Lit(..) | hir::ExprKind::Loop(..) | hir::ExprKind::MethodCall(..) @@ -133,7 +130,7 @@ fn hir_from_snippet(cx: &LateContext<'_, '_>, expr: &hir::Expr<'_>, snippet: Cow /// Prepare a suggestion from an expression. pub fn ast(cx: &EarlyContext<'_>, expr: &ast::Expr, default: &'a str) -> Self { - use syntax::ast::RangeLimits; + use rustc_ast::ast::RangeLimits; let snippet = snippet(cx, expr.span, default); @@ -155,9 +152,10 @@ pub fn ast(cx: &EarlyContext<'_>, expr: &ast::Expr, default: &'a str) -> Self { | ast::ExprKind::ForLoop(..) | ast::ExprKind::Index(..) | ast::ExprKind::InlineAsm(..) + | ast::ExprKind::LlvmInlineAsm(..) | ast::ExprKind::Lit(..) | ast::ExprKind::Loop(..) - | ast::ExprKind::Mac(..) + | ast::ExprKind::MacCall(..) | ast::ExprKind::MethodCall(..) | ast::ExprKind::Paren(..) | ast::ExprKind::Path(..) @@ -327,22 +325,22 @@ pub fn make_unop(op: &str, expr: Sugg<'_>) -> Sugg<'static> { /// parenthesis will always be added for a mix of these. pub fn make_assoc(op: AssocOp, lhs: &Sugg<'_>, rhs: &Sugg<'_>) -> Sugg<'static> { /// Returns `true` if the operator is a shift operator `<<` or `>>`. - fn is_shift(op: &AssocOp) -> bool { - matches!(*op, AssocOp::ShiftLeft | AssocOp::ShiftRight) + fn is_shift(op: AssocOp) -> bool { + matches!(op, AssocOp::ShiftLeft | AssocOp::ShiftRight) } /// Returns `true` if the operator is a arithmetic operator /// (i.e., `+`, `-`, `*`, `/`, `%`). - fn is_arith(op: &AssocOp) -> bool { + fn is_arith(op: AssocOp) -> bool { matches!( - *op, + op, AssocOp::Add | AssocOp::Subtract | AssocOp::Multiply | AssocOp::Divide | AssocOp::Modulus ) } /// Returns `true` if the operator `op` needs parenthesis with the operator /// `other` in the direction `dir`. - fn needs_paren(op: &AssocOp, other: &AssocOp, dir: Associativity) -> bool { + fn needs_paren(op: AssocOp, other: AssocOp, dir: Associativity) -> bool { other.precedence() < op.precedence() || (other.precedence() == op.precedence() && ((op != other && associativity(op) != dir) @@ -351,14 +349,14 @@ fn needs_paren(op: &AssocOp, other: &AssocOp, dir: Associativity) -> bool { || is_shift(other) && is_arith(op) } - let lhs_paren = if let Sugg::BinOp(ref lop, _) = *lhs { - needs_paren(&op, lop, Associativity::Left) + let lhs_paren = if let Sugg::BinOp(lop, _) = *lhs { + needs_paren(op, lop, Associativity::Left) } else { false }; - let rhs_paren = if let Sugg::BinOp(ref rop, _) = *rhs { - needs_paren(&op, rop, Associativity::Right) + let rhs_paren = if let Sugg::BinOp(rop, _) = *rhs { + needs_paren(op, rop, Associativity::Right) } else { false }; @@ -426,10 +424,13 @@ enum Associativity { /// they are considered /// associative. #[must_use] -fn associativity(op: &AssocOp) -> Associativity { - use syntax::util::parser::AssocOp::*; +fn associativity(op: AssocOp) -> Associativity { + use rustc_ast::util::parser::AssocOp::{ + Add, As, Assign, AssignOp, BitAnd, BitOr, BitXor, Colon, Divide, DotDot, DotDotEq, Equal, Greater, + GreaterEqual, LAnd, LOr, Less, LessEqual, Modulus, Multiply, NotEqual, ShiftLeft, ShiftRight, Subtract, + }; - match *op { + match op { Assign | AssignOp(_) => Associativity::Right, Add | BitAnd | BitOr | BitXor | LAnd | LOr | Multiply | As | Colon => Associativity::Both, Divide | Equal | Greater | GreaterEqual | Less | LessEqual | Modulus | NotEqual | ShiftLeft | ShiftRight @@ -440,7 +441,7 @@ fn associativity(op: &AssocOp) -> Associativity { /// Converts a `hir::BinOp` to the corresponding assigning binary operator. fn hirbinop2assignop(op: hir::BinOp) -> AssocOp { - use syntax::token::BinOpToken::*; + use rustc_ast::token::BinOpToken::{And, Caret, Minus, Or, Percent, Plus, Shl, Shr, Slash, Star}; AssocOp::AssignOp(match op.node { hir::BinOpKind::Add => Plus, @@ -467,8 +468,10 @@ fn hirbinop2assignop(op: hir::BinOp) -> AssocOp { /// Converts an `ast::BinOp` to the corresponding assigning binary operator. fn astbinop2assignop(op: ast::BinOp) -> AssocOp { - use syntax::ast::BinOpKind::*; - use syntax::token::BinOpToken; + use rustc_ast::ast::BinOpKind::{ + Add, And, BitAnd, BitOr, BitXor, Div, Eq, Ge, Gt, Le, Lt, Mul, Ne, Or, Rem, Shl, Shr, Sub, + }; + use rustc_ast::token::BinOpToken; AssocOp::AssignOp(match op.node { Add => BinOpToken::Plus, @@ -489,8 +492,7 @@ fn astbinop2assignop(op: ast::BinOp) -> AssocOp { /// before it on its line. fn indentation(cx: &T, span: Span) -> Option { let lo = cx.sess().source_map().lookup_char_pos(span.lo()); - if let Some(line) = lo.file.get_line(lo.line - 1 /* line numbers in `Loc` are 1-based */) { - if let Some((pos, _)) = line.char_indices().find(|&(_, c)| c != ' ' && c != '\t') { + lo.file.get_line(lo.line - 1 /* line numbers in `Loc` are 1-based */).and_then(|line| if let Some((pos, _)) = line.char_indices().find(|&(_, c)| c != ' ' && c != '\t') { // We can mix char and byte positions here because we only consider `[ \t]`. if lo.col == CharPos(pos) { Some(line[..pos].into()) @@ -499,14 +501,11 @@ fn indentation(cx: &T, span: Span) -> Option { } } else { None - } - } else { - None - } + }) } /// Convenience extension trait for `DiagnosticBuilder`. -pub trait DiagnosticBuilderExt<'a, T: LintContext> { +pub trait DiagnosticBuilderExt { /// Suggests to add an attribute to an item. /// /// Correctly handles indentation of the attribute and item. @@ -514,7 +513,7 @@ pub trait DiagnosticBuilderExt<'a, T: LintContext> { /// # Example /// /// ```rust,ignore - /// db.suggest_item_with_attr(cx, item, "#[derive(Default)]"); + /// diag.suggest_item_with_attr(cx, item, "#[derive(Default)]"); /// ``` fn suggest_item_with_attr( &mut self, @@ -527,12 +526,12 @@ fn suggest_item_with_attr( /// Suggest to add an item before another. /// - /// The item should not be indented (expect for inner indentation). + /// The item should not be indented (except for inner indentation). /// /// # Example /// /// ```rust,ignore - /// db.suggest_prepend_item(cx, item, + /// diag.suggest_prepend_item(cx, item, /// "fn foo() { /// bar(); /// }"); @@ -548,12 +547,12 @@ fn suggest_item_with_attr( /// # Example /// /// ```rust,ignore - /// db.suggest_remove_item(cx, item, "remove this") + /// diag.suggest_remove_item(cx, item, "remove this") /// ``` fn suggest_remove_item(&mut self, cx: &T, item: Span, msg: &str, applicability: Applicability); } -impl<'a, 'b, 'c, T: LintContext> DiagnosticBuilderExt<'c, T> for rustc_errors::DiagnosticBuilder<'b> { +impl DiagnosticBuilderExt for rustc_errors::DiagnosticBuilder<'_> { fn suggest_item_with_attr( &mut self, cx: &T,