]> git.lizzy.rs Git - rust.git/blobdiff - clippy_lints/src/open_options.rs
Auto merge of #3946 - rchaser53:issue-3920, r=flip1995
[rust.git] / clippy_lints / src / open_options.rs
index 627608889337ce8aae488a272eac807be94515b5..43c7f3884d3a220fcc4ce67148aa2e2a9c44aa00 100644 (file)
@@ -1,28 +1,30 @@
-use rustc::hir::{Expr, ExprLit, ExprMethodCall};
-use rustc::lint::*;
+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 syntax::ast::LitKind;
-use syntax::codemap::{Span, Spanned};
-use utils::{match_type, paths, span_lint, walk_ptrs_ty_depth};
+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_lint! {
+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,
-    Warn,
+    correctness,
     "nonsensical combination of options for opening a file"
 }
 
-
 #[derive(Copy, Clone)]
 pub struct NonSensical;
 
@@ -30,13 +32,17 @@ impl LintPass for NonSensical {
     fn get_lints(&self) -> LintArray {
         lint_array!(NONSENSICAL_OPEN_OPTIONS)
     }
+
+    fn name(&self) -> &'static str {
+        "OpenOptions"
+    }
 }
 
 impl<'a, 'tcx> LateLintPass<'a, 'tcx> for NonSensical {
     fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, e: &'tcx Expr) {
-        if let ExprMethodCall(ref path, _, ref arguments) = e.node {
-            let (obj_ty, _) = walk_ptrs_ty_depth(cx.tables.expr_ty(&arguments[0]));
-            if path.name == "open" && match_type(cx, obj_ty, &paths::OPEN_OPTIONS) {
+        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) {
                 let mut options = Vec::new();
                 get_open_options(cx, &arguments[0], &mut options);
                 check_open_options(cx, &options, e.span);
@@ -61,18 +67,18 @@ enum OpenOption {
     Append,
 }
 
-fn get_open_options(cx: &LateContext, argument: &Expr, options: &mut Vec<(OpenOption, Argument)>) {
-    if let ExprMethodCall(ref path, _, ref arguments) = argument.node {
-        let (obj_ty, _) = walk_ptrs_ty_depth(cx.tables.expr_ty(&arguments[0]));
+fn get_open_options(cx: &LateContext<'_, '_>, argument: &Expr, options: &mut Vec<(OpenOption, Argument)>) {
+    if let ExprKind::MethodCall(ref path, _, ref arguments) = argument.node {
+        let obj_ty = walk_ptrs_ty(cx.tables.expr_ty(&arguments[0]));
 
         // Only proceed if this is a call on some object of type std::fs::OpenOptions
         if match_type(cx, obj_ty, &paths::OPEN_OPTIONS) && arguments.len() >= 2 {
             let argument_option = match arguments[1].node {
-                ExprLit(ref span) => {
+                ExprKind::Lit(ref span) => {
                     if let Spanned {
                         node: LitKind::Bool(lit),
                         ..
-                    } = **span
+                    } = *span
                     {
                         if lit {
                             Argument::True
@@ -81,14 +87,14 @@ fn get_open_options(cx: &LateContext, argument: &Expr, options: &mut Vec<(OpenOp
                         }
                     } else {
                         return; // The function is called with a literal
-                        // which is not a boolean literal. This is theoretically
-                        // possible, but not very likely.
+                                // which is not a boolean literal. This is theoretically
+                                // possible, but not very likely.
                     }
                 },
                 _ => Argument::Unknown,
             };
 
-            match &*path.name.as_str() {
+            match &*path.ident.as_str() {
                 "create" => {
                     options.push((OpenOption::Create, argument_option));
                 },
@@ -112,7 +118,7 @@ fn get_open_options(cx: &LateContext, argument: &Expr, options: &mut Vec<(OpenOp
     }
 }
 
-fn check_open_options(cx: &LateContext, options: &[(OpenOption, Argument)], span: Span) {
+fn check_open_options(cx: &LateContext<'_, '_>, options: &[(OpenOption, Argument)], span: Span) {
     let (mut create, mut append, mut truncate, mut read, mut write) = (false, false, false, false, false);
     let (mut create_arg, mut append_arg, mut truncate_arg, mut read_arg, mut write_arg) =
         (false, false, false, false, false);
@@ -190,7 +196,12 @@ fn check_open_options(cx: &LateContext, options: &[(OpenOption, Argument)], span
     }
 
     if read && truncate && read_arg && truncate_arg && !(write && write_arg) {
-        span_lint(cx, NONSENSICAL_OPEN_OPTIONS, span, "file opened with \"truncate\" and \"read\"");
+        span_lint(
+            cx,
+            NONSENSICAL_OPEN_OPTIONS,
+            span,
+            "file opened with \"truncate\" and \"read\"",
+        );
     }
     if append && truncate && append_arg && truncate_arg {
         span_lint(