]> git.lizzy.rs Git - rust.git/blobdiff - clippy_lints/src/utils/sugg.rs
Auto merge of #3705 - matthiaskrgr:rustup, r=phansch
[rust.git] / clippy_lints / src / utils / sugg.rs
index 66f18b51f21bb15f14d80b17eb5ed00351fe2d2c..166470876c988fd718f9fccd0bb2950820feeb92 100644 (file)
@@ -1,31 +1,22 @@
-// 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(clippy::missing_docs_in_private_items)]
 
-use crate::rustc::hir;
-use crate::rustc::lint::{EarlyContext, LateContext, LintContext};
-use crate::rustc_errors;
-use crate::rustc_errors::Applicability;
-use crate::syntax::ast;
-use crate::syntax::parse::token;
-use crate::syntax::print::pprust::token_to_string;
-use crate::syntax::source_map::{CharPos, Span};
-use crate::syntax::util::parser::AssocOp;
-use crate::syntax_pos::{BytePos, Pos};
 use crate::utils::{higher, in_macro, snippet, snippet_opt};
 use matches::matches;
+use rustc::hir;
+use rustc::lint::{EarlyContext, LateContext, LintContext};
+use rustc_errors;
+use rustc_errors::Applicability;
 use std;
 use std::borrow::Cow;
 use std::convert::TryInto;
 use std::fmt::Display;
+use syntax::ast;
+use syntax::parse::token;
+use syntax::print::pprust::token_to_string;
+use syntax::source_map::{CharPos, Span};
+use syntax::util::parser::AssocOp;
+use syntax_pos::{BytePos, Pos};
 
 /// A helper type to build suggestion correctly handling parenthesis.
 pub enum Sugg<'a> {
@@ -79,7 +70,8 @@ pub fn hir_opt(cx: &LateContext<'_, '_>, expr: &hir::Expr) -> Option<Self> {
                 | hir::ExprKind::Ret(..)
                 | hir::ExprKind::Struct(..)
                 | hir::ExprKind::Tup(..)
-                | hir::ExprKind::While(..) => Sugg::NonParen(snippet),
+                | hir::ExprKind::While(..)
+                | hir::ExprKind::Err => Sugg::NonParen(snippet),
                 hir::ExprKind::Assign(..) => Sugg::BinOp(AssocOp::Assign, snippet),
                 hir::ExprKind::AssignOp(op, ..) => Sugg::BinOp(hirbinop2assignop(op), snippet),
                 hir::ExprKind::Binary(op, ..) => Sugg::BinOp(AssocOp::from_ast_binop(higher::binop(op.node)), snippet),
@@ -121,7 +113,7 @@ pub fn hir_with_applicability(
 
     /// Prepare a suggestion from an expression.
     pub fn ast(cx: &EarlyContext<'_>, expr: &ast::Expr, default: &'a str) -> Self {
-        use crate::syntax::ast::RangeLimits;
+        use syntax::ast::RangeLimits;
 
         let snippet = snippet(cx, expr.span, default);
 
@@ -158,7 +150,8 @@ pub fn ast(cx: &EarlyContext<'_>, expr: &ast::Expr, default: &'a str) -> Self {
             | ast::ExprKind::Tup(..)
             | ast::ExprKind::Array(..)
             | ast::ExprKind::While(..)
-            | ast::ExprKind::WhileLet(..) => Sugg::NonParen(snippet),
+            | ast::ExprKind::WhileLet(..)
+            | ast::ExprKind::Err => Sugg::NonParen(snippet),
             ast::ExprKind::Range(.., RangeLimits::HalfOpen) => Sugg::BinOp(AssocOp::DotDot, snippet),
             ast::ExprKind::Range(.., RangeLimits::Closed) => Sugg::BinOp(AssocOp::DotDotEq, snippet),
             ast::ExprKind::Assign(..) => Sugg::BinOp(AssocOp::Assign, snippet),
@@ -213,6 +206,17 @@ pub fn mut_addr_deref(self) -> Sugg<'static> {
         make_unop("&mut *", self)
     }
 
+    /// Convenience method to transform suggestion into a return call
+    pub fn make_return(self) -> Sugg<'static> {
+        Sugg::NonParen(Cow::Owned(format!("return {}", self)))
+    }
+
+    /// Convenience method to transform suggestion into a block
+    /// where the suggestion is a trailing expression
+    pub fn blockify(self) -> Sugg<'static> {
+        Sugg::NonParen(Cow::Owned(format!("{{ {} }}", self)))
+    }
+
     /// Convenience method to create the `<lhs>..<rhs>` or `<lhs>...<rhs>`
     /// suggestion.
     #[allow(dead_code)]
@@ -405,7 +409,7 @@ enum Associativity {
 /// they are considered
 /// associative.
 fn associativity(op: &AssocOp) -> Associativity {
-    use crate::syntax::util::parser::AssocOp::*;
+    use syntax::util::parser::AssocOp::*;
 
     match *op {
         ObsoleteInPlace | Assign | AssignOp(_) => Associativity::Right,
@@ -418,7 +422,7 @@ fn associativity(op: &AssocOp) -> Associativity {
 
 /// Convert a `hir::BinOp` to the corresponding assigning binary operator.
 fn hirbinop2assignop(op: hir::BinOp) -> AssocOp {
-    use crate::syntax::parse::token::BinOpToken::*;
+    use syntax::parse::token::BinOpToken::*;
 
     AssocOp::AssignOp(match op.node {
         hir::BinOpKind::Add => Plus,
@@ -445,8 +449,8 @@ fn hirbinop2assignop(op: hir::BinOp) -> AssocOp {
 
 /// Convert an `ast::BinOp` to the corresponding assigning binary operator.
 fn astbinop2assignop(op: ast::BinOp) -> AssocOp {
-    use crate::syntax::ast::BinOpKind::*;
-    use crate::syntax::parse::token::BinOpToken;
+    use syntax::ast::BinOpKind::*;
+    use syntax::parse::token::BinOpToken;
 
     AssocOp::AssignOp(match op.node {
         Add => BinOpToken::Plus,
@@ -543,7 +547,7 @@ fn suggest_item_with_attr<D: Display + ?Sized>(
         if let Some(indent) = indentation(cx, item) {
             let span = item.with_hi(item.lo());
 
-            self.span_suggestion_with_applicability(span, msg, format!("{}\n{}", attr, indent), applicability);
+            self.span_suggestion(span, msg, format!("{}\n{}", attr, indent), applicability);
         }
     }
 
@@ -564,7 +568,7 @@ fn suggest_prepend_item(&mut self, cx: &T, item: Span, msg: &str, new_item: &str
                 })
                 .collect::<String>();
 
-            self.span_suggestion_with_applicability(span, msg, format!("{}\n{}", new_item, indent), applicability);
+            self.span_suggestion(span, msg, format!("{}\n{}", new_item, indent), applicability);
         }
     }
 
@@ -582,6 +586,24 @@ fn suggest_remove_item(&mut self, cx: &T, item: Span, msg: &str, applicability:
             }
         }
 
-        self.span_suggestion_with_applicability(remove_span, msg, String::new(), applicability);
+        self.span_suggestion(remove_span, msg, String::new(), applicability);
+    }
+}
+
+#[cfg(test)]
+mod test {
+    use super::Sugg;
+    use std::borrow::Cow;
+
+    const SUGGESTION: Sugg<'static> = Sugg::NonParen(Cow::Borrowed("function_call()"));
+
+    #[test]
+    fn make_return_transform_sugg_into_a_return_call() {
+        assert_eq!("return function_call()", SUGGESTION.make_return().to_string());
+    }
+
+    #[test]
+    fn blockify_transforms_sugg_into_a_block() {
+        assert_eq!("{ function_call() }", SUGGESTION.blockify().to_string());
     }
 }