//! Contains utility functions to generate suggestions.
#![deny(clippy::missing_docs_in_private_items)]
-use crate::utils::{higher, in_macro, snippet, snippet_opt, snippet_with_macro_callsite};
+use crate::utils::{higher, snippet, snippet_opt, snippet_with_macro_callsite};
use matches::matches;
use rustc::hir;
use rustc::lint::{EarlyContext, LateContext, LintContext};
use std::convert::TryInto;
use std::fmt::Display;
use syntax::ast;
-use syntax::parse::token;
-use syntax::print::pprust::token_to_string;
+use syntax::print::pprust::token_kind_to_string;
use syntax::source_map::{CharPos, Span};
+use syntax::token;
use syntax::util::parser::AssocOp;
use syntax_pos::{BytePos, Pos};
pub fn hir_opt(cx: &LateContext<'_, '_>, expr: &hir::Expr) -> Option<Self> {
snippet_opt(cx, expr.span).map(|snippet| {
let snippet = Cow::Owned(snippet);
- Self::hir_from_snippet(expr, snippet)
+ Self::hir_from_snippet(cx, expr, snippet)
})
}
default: &'a str,
applicability: &mut Applicability,
) -> Self {
- if *applicability != Applicability::Unspecified && in_macro(expr.span) {
+ if *applicability != Applicability::Unspecified && expr.span.from_expansion() {
*applicability = Applicability::MaybeIncorrect;
}
Self::hir_opt(cx, expr).unwrap_or_else(|| {
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(expr, snippet)
+ Self::hir_from_snippet(cx, expr, snippet)
}
/// 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(expr: &hir::Expr, snippet: Cow<'a, str>) -> Self {
- match expr.node {
+ 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,
+ ast::RangeLimits::Closed => AssocOp::DotDotEq,
+ };
+ return Sugg::BinOp(op, snippet);
+ }
+
+ match expr.kind {
hir::ExprKind::AddrOf(..)
| hir::ExprKind::Box(..)
- | hir::ExprKind::Closure(.., _)
- | hir::ExprKind::If(..)
+ | hir::ExprKind::Closure(..)
| hir::ExprKind::Unary(..)
| hir::ExprKind::Match(..) => Sugg::MaybeParen(snippet),
hir::ExprKind::Continue(..)
| hir::ExprKind::Ret(..)
| hir::ExprKind::Struct(..)
| hir::ExprKind::Tup(..)
- | hir::ExprKind::While(..)
| hir::ExprKind::DropTemps(_)
| hir::ExprKind::Err => Sugg::NonParen(snippet),
hir::ExprKind::Assign(..) => Sugg::BinOp(AssocOp::Assign, snippet),
let snippet = snippet(cx, expr.span, default);
- match expr.node {
+ match expr.kind {
ast::ExprKind::AddrOf(..)
| ast::ExprKind::Box(..)
| ast::ExprKind::Closure(..)
| ast::ExprKind::If(..)
- | ast::ExprKind::IfLet(..)
- | ast::ExprKind::ObsoleteInPlace(..)
+ | ast::ExprKind::Let(..)
| ast::ExprKind::Unary(..)
| ast::ExprKind::Match(..) => Sugg::MaybeParen(snippet),
ast::ExprKind::Async(..)
| ast::ExprKind::Tup(..)
| ast::ExprKind::Array(..)
| ast::ExprKind::While(..)
- | ast::ExprKind::WhileLet(..)
| ast::ExprKind::Await(..)
| ast::ExprKind::Err => Sugg::NonParen(snippet),
ast::ExprKind::Range(.., RangeLimits::HalfOpen) => Sugg::BinOp(AssocOp::DotDot, snippet),
rhs
),
AssocOp::Assign => format!("{} = {}", lhs, rhs),
- AssocOp::ObsoleteInPlace => format!("in ({}) {}", lhs, rhs),
- AssocOp::AssignOp(op) => format!("{} {}= {}", lhs, token_to_string(&token::BinOp(op)), rhs),
+ AssocOp::AssignOp(op) => format!("{} {}= {}", lhs, token_kind_to_string(&token::BinOp(op)), rhs),
AssocOp::As => format!("{} as {}", lhs, rhs),
AssocOp::DotDot => format!("{}..{}", lhs, rhs),
AssocOp::DotDotEq => format!("{}..={}", lhs, rhs),
/// Chained `as` and explicit `:` type coercion never need inner parenthesis so
/// they are considered
/// associative.
+#[must_use]
fn associativity(op: &AssocOp) -> Associativity {
use syntax::util::parser::AssocOp::*;
match *op {
- ObsoleteInPlace | Assign | AssignOp(_) => Associativity::Right,
+ 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
| Subtract => Associativity::Left,
/// Converts a `hir::BinOp` to the corresponding assigning binary operator.
fn hirbinop2assignop(op: hir::BinOp) -> AssocOp {
- use syntax::parse::token::BinOpToken::*;
+ use syntax::token::BinOpToken::*;
AssocOp::AssignOp(match op.node {
hir::BinOpKind::Add => Plus,
/// Converts an `ast::BinOp` to the corresponding assigning binary operator.
fn astbinop2assignop(op: ast::BinOp) -> AssocOp {
use syntax::ast::BinOpKind::*;
- use syntax::parse::token::BinOpToken;
+ use syntax::token::BinOpToken;
AssocOp::AssignOp(match op.node {
Add => BinOpToken::Plus,
/// Returns the indentation before `span` if there are nothing but `[ \t]`
/// before it on its line.
-fn indentation<'a, T: LintContext<'a>>(cx: &T, span: Span) -> Option<String> {
+fn indentation<T: LintContext>(cx: &T, span: Span) -> Option<String> {
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') {
}
/// Convenience extension trait for `DiagnosticBuilder`.
-pub trait DiagnosticBuilderExt<'a, T: LintContext<'a>> {
+pub trait DiagnosticBuilderExt<'a, T: LintContext> {
/// Suggests to add an attribute to an item.
///
/// Correctly handles indentation of the attribute and item.
fn suggest_remove_item(&mut self, cx: &T, item: Span, msg: &str, applicability: Applicability);
}
-impl<'a, 'b, 'c, T: LintContext<'c>> DiagnosticBuilderExt<'c, T> for rustc_errors::DiagnosticBuilder<'b> {
+impl<'a, 'b, 'c, T: LintContext> DiagnosticBuilderExt<'c, T> for rustc_errors::DiagnosticBuilder<'b> {
fn suggest_item_with_attr<D: Display + ?Sized>(
&mut self,
cx: &T,