+// Copyright 2014-2018 The Rust Project Developers. See the COPYRIGHT
+// file at the top-level directory of this distribution.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+
//! Contains utility functions to generate suggestions.
-#![deny(missing_docs_in_private_items)]
-// currently ignores lifetimes and generics
-#![allow(use_self)]
+#![deny(clippy::missing_docs_in_private_items)]
use matches::matches;
-use rustc::hir;
-use rustc::lint::{EarlyContext, LateContext, LintContext};
-use rustc_errors;
+use crate::rustc::hir;
+use crate::rustc::lint::{EarlyContext, LateContext, LintContext};
+use crate::rustc_errors;
use std::borrow::Cow;
use std::fmt::Display;
use std;
-use syntax::codemap::{CharPos, Span};
-use syntax::parse::token;
-use syntax::print::pprust::token_to_string;
-use syntax::util::parser::AssocOp;
-use syntax::ast;
+use crate::syntax::source_map::{CharPos, Span};
+use crate::syntax::parse::token;
+use crate::syntax::print::pprust::token_to_string;
+use crate::syntax::util::parser::AssocOp;
+use crate::syntax::ast;
use crate::utils::{higher, snippet, snippet_opt};
-use syntax_pos::{BytePos, Pos};
+use crate::syntax_pos::{BytePos, Pos};
+use crate::rustc_errors::Applicability;
/// A helper type to build suggestion correctly handling parenthesis.
pub enum Sugg<'a> {
}
}
-#[allow(wrong_self_convention)] // ok, because of the function `as_ty` method
+#[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<Self> {
/// Prepare a suggestion from an expression.
pub fn ast(cx: &EarlyContext<'_>, expr: &ast::Expr, default: &'a str) -> Self {
- use syntax::ast::RangeLimits;
+ use crate::syntax::ast::RangeLimits;
let snippet = snippet(cx, expr.span, default);
ast::ExprKind::Block(..) |
ast::ExprKind::Break(..) |
ast::ExprKind::Call(..) |
- ast::ExprKind::Catch(..) |
ast::ExprKind::Continue(..) |
ast::ExprKind::Yield(..) |
ast::ExprKind::Field(..) |
ast::ExprKind::Ret(..) |
ast::ExprKind::Struct(..) |
ast::ExprKind::Try(..) |
+ ast::ExprKind::TryBlock(..) |
ast::ExprKind::Tup(..) |
ast::ExprKind::Array(..) |
ast::ExprKind::While(..) |
/// Convenience method to create the `<lhs>..<rhs>` or `<lhs>...<rhs>`
/// suggestion.
+ #[allow(dead_code)]
pub fn range(self, end: &Self, limit: ast::RangeLimits) -> Sugg<'static> {
match limit {
ast::RangeLimits::HalfOpen => make_assoc(AssocOp::DotDot, &self, end),
Sugg::BinOp(op, sugg.into())
}
-/// Convinience wrapper arround `make_assoc` and `AssocOp::from_ast_binop`.
+/// Convenience wrapper around `make_assoc` and `AssocOp::from_ast_binop`.
pub fn make_binop(op: ast::BinOpKind, lhs: &Sugg<'_>, rhs: &Sugg<'_>) -> Sugg<'static> {
make_assoc(AssocOp::from_ast_binop(op), lhs, rhs)
}
/// they are considered
/// associative.
fn associativity(op: &AssocOp) -> Associativity {
- use syntax::util::parser::AssocOp::*;
+ use crate::syntax::util::parser::AssocOp::*;
match *op {
ObsoleteInPlace | Assign | AssignOp(_) => Associativity::Right,
/// Convert a `hir::BinOp` to the corresponding assigning binary operator.
fn hirbinop2assignop(op: hir::BinOp) -> AssocOp {
- use syntax::parse::token::BinOpToken::*;
+ use crate::syntax::parse::token::BinOpToken::*;
AssocOp::AssignOp(match op.node {
hir::BinOpKind::Add => Plus,
/// Convert 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 crate::syntax::ast::BinOpKind::*;
+ use crate::syntax::parse::token::BinOpToken;
AssocOp::AssignOp(match op.node {
Add => BinOpToken::Plus,
/// Return 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> {
- let lo = cx.sess().codemap().lookup_char_pos(span.lo());
+ 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 */)
{
/// ```rust,ignore
/// db.suggest_item_with_attr(cx, item, "#[derive(Default)]");
/// ```
- fn suggest_item_with_attr<D: Display + ?Sized>(&mut self, cx: &T, item: Span, msg: &str, attr: &D);
+ fn suggest_item_with_attr<D: Display + ?Sized>(&mut self, cx: &T, item: Span, msg: &str, attr: &D, applicability: Applicability);
/// Suggest to add an item before another.
///
/// bar();
/// }");
/// ```
- fn suggest_prepend_item(&mut self, cx: &T, item: Span, msg: &str, new_item: &str);
+ fn suggest_prepend_item(&mut self, cx: &T, item: Span, msg: &str, new_item: &str, applicability: Applicability);
/// Suggest to completely remove an item.
///
/// ```rust,ignore
/// db.suggest_remove_item(cx, item, "remove this")
/// ```
- fn suggest_remove_item(&mut self, cx: &T, item: Span, msg: &str);
+ 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> {
- fn suggest_item_with_attr<D: Display + ?Sized>(&mut self, cx: &T, item: Span, msg: &str, attr: &D) {
+ fn suggest_item_with_attr<D: Display + ?Sized>(&mut self, cx: &T, item: Span, msg: &str, attr: &D, applicability: Applicability) {
if let Some(indent) = indentation(cx, item) {
let span = item.with_hi(item.lo());
- self.span_suggestion(span, msg, format!("{}\n{}", attr, indent));
+ self.span_suggestion_with_applicability(
+ span,
+ msg,
+ format!("{}\n{}", attr, indent),
+ applicability,
+ );
}
}
- fn suggest_prepend_item(&mut self, cx: &T, item: Span, msg: &str, new_item: &str) {
+ fn suggest_prepend_item(&mut self, cx: &T, item: Span, msg: &str, new_item: &str, applicability: Applicability) {
if let Some(indent) = indentation(cx, item) {
let span = item.with_hi(item.lo());
})
.collect::<String>();
- self.span_suggestion(span, msg, format!("{}\n{}", new_item, indent));
+ self.span_suggestion_with_applicability(
+ span,
+ msg,
+ format!("{}\n{}", new_item, indent),
+ applicability,
+ );
}
}
- fn suggest_remove_item(&mut self, cx: &T, item: Span, msg: &str) {
+ fn suggest_remove_item(&mut self, cx: &T, item: Span, msg: &str, applicability: Applicability) {
let mut remove_span = item;
- let hi = cx.sess().codemap().next_point(remove_span).hi();
- let fmpos = cx.sess().codemap().lookup_byte_offset(hi);
+ let hi = cx.sess().source_map().next_point(remove_span).hi();
+ let fmpos = cx.sess().source_map().lookup_byte_offset(hi);
if let Some(ref src) = fmpos.fm.src {
let non_whitespace_offset = src[fmpos.pos.to_usize()..].find(|c| c != ' ' && c != '\t' && c != '\n');
}
}
- self.span_suggestion(remove_span, msg, String::new());
+ self.span_suggestion_with_applicability(
+ remove_span,
+ msg,
+ String::new(),
+ applicability,
+ );
}
}