]> git.lizzy.rs Git - rust.git/blobdiff - clippy_lints/src/open_options.rs
Auto merge of #4551 - mikerite:fix-ice-reporting, r=llogiq
[rust.git] / clippy_lints / src / open_options.rs
index f6773dcb158c1abeaf2a6b38f7b36605acecd24e..5a3af2c3773fdf31e1829778eca8de3eff665777 100644 (file)
@@ -1,51 +1,37 @@
-// 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.
-
 use crate::utils::{match_type, paths, span_lint, walk_ptrs_ty};
 use rustc::hir::{Expr, ExprKind};
 use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
-use rustc::{declare_tool_lint, lint_array};
+use rustc::{declare_lint_pass, declare_tool_lint};
 use syntax::ast::LitKind;
 use syntax::source_map::{Span, Spanned};
 
-/// **What it does:** Checks for duplicate open options as well as combinations
-/// that make no sense.
-///
-/// **Why is this bad?** In the best case, the code will be harder to read than
-/// necessary. I don't know the worst case.
-///
-/// **Known problems:** None.
-///
-/// **Example:**
-/// ```rust
-/// OpenOptions::new().read(true).truncate(true)
-/// ```
 declare_clippy_lint! {
+    /// **What it does:** Checks for duplicate open options as well as combinations
+    /// that make no sense.
+    ///
+    /// **Why is this bad?** In the best case, the code will be harder to read than
+    /// necessary. I don't know the worst case.
+    ///
+    /// **Known problems:** None.
+    ///
+    /// **Example:**
+    /// ```rust
+    /// use std::fs::OpenOptions;
+    ///
+    /// OpenOptions::new().read(true).truncate(true);
+    /// ```
     pub NONSENSICAL_OPEN_OPTIONS,
     correctness,
     "nonsensical combination of options for opening a file"
 }
 
-#[derive(Copy, Clone)]
-pub struct NonSensical;
-
-impl LintPass for NonSensical {
-    fn get_lints(&self) -> LintArray {
-        lint_array!(NONSENSICAL_OPEN_OPTIONS)
-    }
-}
+declare_lint_pass!(OpenOptions => [NONSENSICAL_OPEN_OPTIONS]);
 
-impl<'a, 'tcx> LateLintPass<'a, 'tcx> for NonSensical {
+impl<'a, 'tcx> LateLintPass<'a, 'tcx> for OpenOptions {
     fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, e: &'tcx Expr) {
         if let ExprKind::MethodCall(ref path, _, ref arguments) = e.node {
             let obj_ty = walk_ptrs_ty(cx.tables.expr_ty(&arguments[0]));
-            if path.ident.name == "open" && match_type(cx, obj_ty, &paths::OPEN_OPTIONS) {
+            if path.ident.name == sym!(open) && match_type(cx, obj_ty, &paths::OPEN_OPTIONS) {
                 let mut options = Vec::new();
                 get_open_options(cx, &arguments[0], &mut options);
                 check_open_options(cx, &options, e.span);
@@ -81,7 +67,7 @@ fn get_open_options(cx: &LateContext<'_, '_>, argument: &Expr, options: &mut Vec
                     if let Spanned {
                         node: LitKind::Bool(lit),
                         ..
-                    } = **span
+                    } = *span
                     {
                         if lit {
                             Argument::True
@@ -141,7 +127,7 @@ fn check_open_options(cx: &LateContext<'_, '_>, options: &[(OpenOption, Argument
                 } else {
                     create = true
                 }
-                create_arg = create_arg || (arg == Argument::True);;
+                create_arg = create_arg || (arg == Argument::True);
             },
             (OpenOption::Append, arg) => {
                 if append {
@@ -154,7 +140,7 @@ fn check_open_options(cx: &LateContext<'_, '_>, options: &[(OpenOption, Argument
                 } else {
                     append = true
                 }
-                append_arg = append_arg || (arg == Argument::True);;
+                append_arg = append_arg || (arg == Argument::True);
             },
             (OpenOption::Truncate, arg) => {
                 if truncate {
@@ -180,7 +166,7 @@ fn check_open_options(cx: &LateContext<'_, '_>, options: &[(OpenOption, Argument
                 } else {
                     read = true
                 }
-                read_arg = read_arg || (arg == Argument::True);;
+                read_arg = read_arg || (arg == Argument::True);
             },
             (OpenOption::Write, arg) => {
                 if write {
@@ -193,7 +179,7 @@ fn check_open_options(cx: &LateContext<'_, '_>, options: &[(OpenOption, Argument
                 } else {
                     write = true
                 }
-                write_arg = write_arg || (arg == Argument::True);;
+                write_arg = write_arg || (arg == Argument::True);
             },
         }
     }