|
| 1 | +use rustc::lint::*; |
| 2 | +use rustc_front::hir::{Expr, ExprMethodCall, ExprLit}; |
| 3 | +use utils::{walk_ptrs_ty_depth, match_type, span_lint, OPEN_OPTIONS_PATH}; |
| 4 | +use syntax::codemap::{Span, Spanned}; |
| 5 | +use syntax::ast::Lit_::LitBool; |
| 6 | + |
| 7 | +declare_lint! { |
| 8 | + pub NONSENSICAL_OPEN_OPTIONS, |
| 9 | + Warn, |
| 10 | + "nonsensical combination of options for opening a file" |
| 11 | +} |
| 12 | + |
| 13 | + |
| 14 | +#[derive(Copy,Clone)] |
| 15 | +pub struct NonSensicalOpenOptions; |
| 16 | + |
| 17 | +impl LintPass for NonSensicalOpenOptions { |
| 18 | + fn get_lints(&self) -> LintArray { |
| 19 | + lint_array!(NONSENSICAL_OPEN_OPTIONS) |
| 20 | + } |
| 21 | +} |
| 22 | + |
| 23 | +impl LateLintPass for NonSensicalOpenOptions { |
| 24 | + fn check_expr(&mut self, cx: &LateContext, e: &Expr) { |
| 25 | + if let ExprMethodCall(ref name, _, ref arguments) = e.node { |
| 26 | + let (obj_ty, _) = walk_ptrs_ty_depth(cx.tcx.expr_ty(&arguments[0])); |
| 27 | + if name.node.as_str() == "open" && match_type(cx, obj_ty, &OPEN_OPTIONS_PATH){ |
| 28 | + let mut options = Vec::new(); |
| 29 | + get_open_options(cx, &arguments[0], &mut options); |
| 30 | + check_open_options(cx, &options, e.span); |
| 31 | + } |
| 32 | + } |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +#[derive(Debug)] |
| 37 | +enum Argument { |
| 38 | + True, |
| 39 | + False, |
| 40 | + Unknown |
| 41 | +} |
| 42 | + |
| 43 | +#[derive(Debug)] |
| 44 | +enum OpenOption { |
| 45 | + Write, |
| 46 | + Read, |
| 47 | + Truncate, |
| 48 | + Create, |
| 49 | + Append |
| 50 | +} |
| 51 | + |
| 52 | +fn get_open_options(cx: &LateContext, argument: &Expr, options: &mut Vec<(OpenOption, Argument)>) { |
| 53 | + if let ExprMethodCall(ref name, _, ref arguments) = argument.node { |
| 54 | + let (obj_ty, _) = walk_ptrs_ty_depth(cx.tcx.expr_ty(&arguments[0])); |
| 55 | + |
| 56 | + // Only proceed if this is a call on some object of type std::fs::OpenOptions |
| 57 | + if match_type(cx, obj_ty, &OPEN_OPTIONS_PATH) && arguments.len() >= 2 { |
| 58 | + |
| 59 | + let argument_option = match arguments[1].node { |
| 60 | + ExprLit(ref span) => { |
| 61 | + if let Spanned {node: LitBool(lit), span: _} = **span { |
| 62 | + if lit {Argument::True} else {Argument::False} |
| 63 | + } else { |
| 64 | + return; // The function is called with a literal |
| 65 | + // which is not a boolean literal. This is theoretically |
| 66 | + // possible, but not very likely. |
| 67 | + } |
| 68 | + }, |
| 69 | + _ => { |
| 70 | + Argument::Unknown |
| 71 | + } |
| 72 | + }; |
| 73 | + |
| 74 | + match &*name.node.as_str() { |
| 75 | + "create" => { |
| 76 | + options.push((OpenOption::Create, argument_option)); |
| 77 | + }, |
| 78 | + "append" => { |
| 79 | + options.push((OpenOption::Append, argument_option)); |
| 80 | + }, |
| 81 | + "truncate" => { |
| 82 | + options.push((OpenOption::Truncate, argument_option)); |
| 83 | + }, |
| 84 | + "read" => { |
| 85 | + options.push((OpenOption::Read, argument_option)); |
| 86 | + }, |
| 87 | + "write" => { |
| 88 | + options.push((OpenOption::Write, argument_option)); |
| 89 | + }, |
| 90 | + _ => {} |
| 91 | + } |
| 92 | + |
| 93 | + get_open_options(cx, &arguments[0], options); |
| 94 | + } |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +fn check_for_duplicates(cx: &LateContext, options: &[(OpenOption, Argument)], span: Span) { |
| 99 | + // This code is almost duplicated (oh, the irony), but I haven't found a way to unify it. |
| 100 | + if options.iter().filter(|o| if let (OpenOption::Create, _) = **o {true} else {false}).count() > 1 { |
| 101 | + span_lint(cx, NONSENSICAL_OPEN_OPTIONS, span, "The method \"create\" \ |
| 102 | + is called more than once"); |
| 103 | + } |
| 104 | + if options.iter().filter(|o| if let (OpenOption::Append, _) = **o {true} else {false}).count() > 1 { |
| 105 | + span_lint(cx, NONSENSICAL_OPEN_OPTIONS, span, "The method \"append\" \ |
| 106 | + is called more than once"); |
| 107 | + } |
| 108 | + if options.iter().filter(|o| if let (OpenOption::Truncate, _) = **o {true} else {false}).count() > 1 { |
| 109 | + span_lint(cx, NONSENSICAL_OPEN_OPTIONS, span, "The method \"truncate\" \ |
| 110 | + is called more than once"); |
| 111 | + } |
| 112 | + if options.iter().filter(|o| if let (OpenOption::Read, _) = **o {true} else {false}).count() > 1 { |
| 113 | + span_lint(cx, NONSENSICAL_OPEN_OPTIONS, span, "The method \"read\" \ |
| 114 | + is called more than once"); |
| 115 | + } |
| 116 | + if options.iter().filter(|o| if let (OpenOption::Write, _) = **o {true} else {false}).count() > 1 { |
| 117 | + span_lint(cx, NONSENSICAL_OPEN_OPTIONS, span, "The method \"write\" \ |
| 118 | + is called more than once"); |
| 119 | + } |
| 120 | +} |
| 121 | + |
| 122 | +fn check_for_inconsistencies(cx: &LateContext, options: &[(OpenOption, Argument)], span: Span) { |
| 123 | + // Truncate + read makes no sense. |
| 124 | + if options.iter().filter(|o| if let (OpenOption::Read, Argument::True) = **o {true} else {false}).count() > 0 && |
| 125 | + options.iter().filter(|o| if let (OpenOption::Truncate, Argument::True) = **o {true} else {false}).count() > 0 { |
| 126 | + span_lint(cx, NONSENSICAL_OPEN_OPTIONS, span, "File opened with \"truncate\" and \"read\""); |
| 127 | + } |
| 128 | + |
| 129 | + // Append + truncate makes no sense. |
| 130 | + if options.iter().filter(|o| if let (OpenOption::Append, Argument::True) = **o {true} else {false}).count() > 0 && |
| 131 | + options.iter().filter(|o| if let (OpenOption::Truncate, Argument::True) = **o {true} else {false}).count() > 0 { |
| 132 | + span_lint(cx, NONSENSICAL_OPEN_OPTIONS, span, "File opened with \"append\" and \"truncate\""); |
| 133 | + } |
| 134 | +} |
| 135 | + |
| 136 | +fn check_open_options(cx: &LateContext, options: &[(OpenOption, Argument)], span: Span) { |
| 137 | + check_for_duplicates(cx, options, span); |
| 138 | + check_for_inconsistencies(cx, options, span); |
| 139 | +} |
0 commit comments