]> git.lizzy.rs Git - rust.git/blobdiff - clippy_lints/src/map_unit_fn.rs
Auto merge of #4551 - mikerite:fix-ice-reporting, r=llogiq
[rust.git] / clippy_lints / src / map_unit_fn.rs
index 5318badf36c6313c48f8ef4ad83c3fd2f6af4de0..0df14c2664aefadd814112099896ab245d55f406 100644 (file)
@@ -1,6 +1,5 @@
 use crate::utils::paths;
-use crate::utils::sym;
-use crate::utils::{in_macro_or_desugar, iter_input_pats, match_type, method_chain_args, snippet, span_lint_and_then};
+use crate::utils::{iter_input_pats, match_type, method_chain_args, snippet, span_lint_and_then};
 use if_chain::if_chain;
 use rustc::hir;
 use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
     /// **Example:**
     ///
     /// ```rust
-    /// let x: Option<&str> = do_stuff();
+    /// # fn do_stuff() -> Option<String> { Some(String::new()) }
+    /// # fn log_err_msg(foo: String) -> Option<String> { Some(foo) }
+    /// # fn format_msg(foo: String) -> String { String::new() }
+    /// let x: Option<String> = do_stuff();
     /// x.map(log_err_msg);
-    /// x.map(|msg| log_err_msg(format_msg(msg)))
+    /// # let x: Option<String> = do_stuff();
+    /// x.map(|msg| log_err_msg(format_msg(msg)));
     /// ```
     ///
     /// The correct use would be:
     ///
     /// ```rust
-    /// let x: Option<&str> = do_stuff();
+    /// # fn do_stuff() -> Option<String> { Some(String::new()) }
+    /// # fn log_err_msg(foo: String) -> Option<String> { Some(foo) }
+    /// # fn format_msg(foo: String) -> String { String::new() }
+    /// let x: Option<String> = do_stuff();
     /// if let Some(msg) = x {
-    ///     log_err_msg(msg)
+    ///     log_err_msg(msg);
     /// }
+    ///
+    /// # let x: Option<String> = do_stuff();
     /// if let Some(msg) = x {
-    ///     log_err_msg(format_msg(msg))
+    ///     log_err_msg(format_msg(msg));
     /// }
     /// ```
     pub OPTION_MAP_UNIT_FN,
     /// **Example:**
     ///
     /// ```rust
-    /// let x: Result<&str, &str> = do_stuff();
+    /// # fn do_stuff() -> Result<String, String> { Ok(String::new()) }
+    /// # fn log_err_msg(foo: String) -> Result<String, String> { Ok(foo) }
+    /// # fn format_msg(foo: String) -> String { String::new() }
+    /// let x: Result<String, String> = do_stuff();
     /// x.map(log_err_msg);
-    /// x.map(|msg| log_err_msg(format_msg(msg)))
+    /// # let x: Result<String, String> = do_stuff();
+    /// x.map(|msg| log_err_msg(format_msg(msg)));
     /// ```
     ///
     /// The correct use would be:
     ///
     /// ```rust
-    /// let x: Result<&str, &str> = do_stuff();
+    /// # fn do_stuff() -> Result<String, String> { Ok(String::new()) }
+    /// # fn log_err_msg(foo: String) -> Result<String, String> { Ok(foo) }
+    /// # fn format_msg(foo: String) -> String { String::new() }
+    /// let x: Result<String, String> = do_stuff();
     /// if let Ok(msg) = x {
-    ///     log_err_msg(msg)
-    /// }
+    ///     log_err_msg(msg);
+    /// };
+    /// # let x: Result<String, String> = do_stuff();
     /// if let Ok(msg) = x {
-    ///     log_err_msg(format_msg(msg))
-    /// }
+    ///     log_err_msg(format_msg(msg));
+    /// };
     /// ```
     pub RESULT_MAP_UNIT_FN,
     complexity,
@@ -145,7 +161,10 @@ fn reduce_unit_expression<'a>(cx: &LateContext<'_, '_>, expr: &'a hir::Expr) ->
     }
 }
 
-fn unit_closure<'a, 'tcx>(cx: &LateContext<'a, 'tcx>, expr: &'a hir::Expr) -> Option<(&'tcx hir::Arg, &'a hir::Expr)> {
+fn unit_closure<'a, 'tcx>(
+    cx: &LateContext<'a, 'tcx>,
+    expr: &'a hir::Expr,
+) -> Option<(&'tcx hir::Param, &'a hir::Expr)> {
     if let hir::ExprKind::Closure(_, ref decl, inner_expr_id, _, _) = expr.node {
         let body = cx.tcx.hir().body(inner_expr_id);
         let body_expr = &body.value;
@@ -186,9 +205,9 @@ fn suggestion_msg(function_type: &str, map_type: &str) -> String {
 fn lint_map_unit_fn(cx: &LateContext<'_, '_>, stmt: &hir::Stmt, expr: &hir::Expr, map_args: &[hir::Expr]) {
     let var_arg = &map_args[0];
 
-    let (map_type, variant, lint) = if match_type(cx, cx.tables.expr_ty(var_arg), &*paths::OPTION) {
+    let (map_type, variant, lint) = if match_type(cx, cx.tables.expr_ty(var_arg), &paths::OPTION) {
         ("Option", "Some", OPTION_MAP_UNIT_FN)
-    } else if match_type(cx, cx.tables.expr_ty(var_arg), &*paths::RESULT) {
+    } else if match_type(cx, cx.tables.expr_ty(var_arg), &paths::RESULT) {
         ("Result", "Ok", RESULT_MAP_UNIT_FN)
     } else {
         return;
@@ -241,12 +260,12 @@ fn lint_map_unit_fn(cx: &LateContext<'_, '_>, stmt: &hir::Stmt, expr: &hir::Expr
 
 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for MapUnit {
     fn check_stmt(&mut self, cx: &LateContext<'_, '_>, stmt: &hir::Stmt) {
-        if in_macro_or_desugar(stmt.span) {
+        if stmt.span.from_expansion() {
             return;
         }
 
         if let hir::StmtKind::Semi(ref expr) = stmt.node {
-            if let Some(arglists) = method_chain_args(expr, &[*sym::map]) {
+            if let Some(arglists) = method_chain_args(expr, &["map"]) {
                 lint_map_unit_fn(cx, stmt, expr, arglists[0]);
             }
         }