]> git.lizzy.rs Git - rust.git/blobdiff - clippy_lints/src/infallible_destructuring_match.rs
Auto merge of #4551 - mikerite:fix-ice-reporting, r=llogiq
[rust.git] / clippy_lints / src / infallible_destructuring_match.rs
index a085f32ed841fd51c3c31e96ec842db3e80c664b..a977a827321d364256bb54a682ffc70a508ab0d0 100644 (file)
@@ -1,54 +1,48 @@
-use super::utils::{get_arg_name, match_var, remove_blocks, snippet, span_lint_and_sugg};
+use super::utils::{get_arg_name, match_var, remove_blocks, snippet_with_applicability, span_lint_and_sugg};
+use if_chain::if_chain;
 use rustc::hir::*;
 use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
-use rustc::{declare_lint, lint_array};
-use if_chain::if_chain;
+use rustc::{declare_lint_pass, declare_tool_lint};
+use rustc_errors::Applicability;
 
-/// **What it does:** Checks for matches being used to destructure a single-variant enum
-/// or tuple struct where a `let` will suffice.
-///
-/// **Why is this bad?** Just readability – `let` doesn't nest, whereas a `match` does.
-///
-/// **Known problems:** None.
-///
-/// **Example:**
-/// ```rust
-/// enum Wrapper {
-///     Data(i32),
-/// }
-///
-/// let wrapper = Wrapper::Data(42);
-///
-/// let data = match wrapper {
-///     Wrapper::Data(i) => i,
-/// };
-/// ```
-///
-/// The correct use would be:
-/// ```rust
-/// enum Wrapper {
-///     Data(i32),
-/// }
-///
-/// let wrapper = Wrapper::Data(42);
-/// let Wrapper::Data(data) = wrapper;
-/// ```
 declare_clippy_lint! {
+    /// **What it does:** Checks for matches being used to destructure a single-variant enum
+    /// or tuple struct where a `let` will suffice.
+    ///
+    /// **Why is this bad?** Just readability – `let` doesn't nest, whereas a `match` does.
+    ///
+    /// **Known problems:** None.
+    ///
+    /// **Example:**
+    /// ```rust
+    /// enum Wrapper {
+    ///     Data(i32),
+    /// }
+    ///
+    /// let wrapper = Wrapper::Data(42);
+    ///
+    /// let data = match wrapper {
+    ///     Wrapper::Data(i) => i,
+    /// };
+    /// ```
+    ///
+    /// The correct use would be:
+    /// ```rust
+    /// enum Wrapper {
+    ///     Data(i32),
+    /// }
+    ///
+    /// let wrapper = Wrapper::Data(42);
+    /// let Wrapper::Data(data) = wrapper;
+    /// ```
     pub INFALLIBLE_DESTRUCTURING_MATCH,
     style,
     "a match statement with a single infallible arm instead of a `let`"
 }
 
-#[derive(Copy, Clone, Default)]
-pub struct Pass;
-
-impl LintPass for Pass {
-    fn get_lints(&self) -> LintArray {
-        lint_array!(INFALLIBLE_DESTRUCTURING_MATCH)
-    }
-}
+declare_lint_pass!(InfallibleDestructingMatch => [INFALLIBLE_DESTRUCTURING_MATCH]);
 
-impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass {
+impl<'a, 'tcx> LateLintPass<'a, 'tcx> for InfallibleDestructingMatch {
     fn check_local(&mut self, cx: &LateContext<'a, 'tcx>, local: &'tcx Local) {
         if_chain! {
             if let Some(ref expr) = local.init;
@@ -61,6 +55,7 @@ fn check_local(&mut self, cx: &LateContext<'a, 'tcx>, local: &'tcx Local) {
             if match_var(body, arg);
 
             then {
+                let mut applicability = Applicability::MachineApplicable;
                 span_lint_and_sugg(
                     cx,
                     INFALLIBLE_DESTRUCTURING_MATCH,
@@ -70,10 +65,11 @@ fn check_local(&mut self, cx: &LateContext<'a, 'tcx>, local: &'tcx Local) {
                     "try this",
                     format!(
                         "let {}({}) = {};",
-                        snippet(cx, variant_name.span, ".."),
-                        snippet(cx, local.pat.span, ".."),
-                        snippet(cx, target.span, ".."),
+                        snippet_with_applicability(cx, variant_name.span, "..", &mut applicability),
+                        snippet_with_applicability(cx, local.pat.span, "..", &mut applicability),
+                        snippet_with_applicability(cx, target.span, "..", &mut applicability),
                     ),
+                    applicability,
                 );
             }
         }