From b7c6c30c8835254e2cb7c5fada886ffa7685bbd4 Mon Sep 17 00:00:00 2001 From: Pyriphlegethon Date: Wed, 7 Oct 2015 17:15:44 +0200 Subject: [PATCH] Change lint description --- README.md | 2 +- src/open_options.rs | 48 ++++++++++++++++++++++----------------------- 2 files changed, 25 insertions(+), 25 deletions(-) diff --git a/README.md b/README.md index 96465d9fd35..6c6849ba105 100644 --- a/README.md +++ b/README.md @@ -42,7 +42,7 @@ name [needless_range_loop](https://github.com/Manishearth/rust-clippy/wiki#needless_range_loop) | warn | for-looping over a range of indices where an iterator over items would do [needless_return](https://github.com/Manishearth/rust-clippy/wiki#needless_return) | warn | using a return statement like `return expr;` where an expression would suffice [non_ascii_literal](https://github.com/Manishearth/rust-clippy/wiki#non_ascii_literal) | allow | using any literal non-ASCII chars in a string literal; suggests using the \\u escape instead -[nonsensical_open_options](https://github.com/Manishearth/rust-clippy/wiki#nonsensical_open_options) | warn | The options used for opening a file are nonsensical +[nonsensical_open_options](https://github.com/Manishearth/rust-clippy/wiki#nonsensical_open_options) | warn | nonsensical combination of options for opening a file [option_unwrap_used](https://github.com/Manishearth/rust-clippy/wiki#option_unwrap_used) | allow | using `Option.unwrap()`, which should at least get a better message using `expect()` [precedence](https://github.com/Manishearth/rust-clippy/wiki#precedence) | warn | catches operations where precedence may be unclear. See the wiki for a list of cases caught [ptr_arg](https://github.com/Manishearth/rust-clippy/wiki#ptr_arg) | allow | fn arguments of the type `&Vec<...>` or `&String`, suggesting to use `&[...]` or `&str` instead, respectively diff --git a/src/open_options.rs b/src/open_options.rs index d91305c36c2..76a2eeef1ba 100644 --- a/src/open_options.rs +++ b/src/open_options.rs @@ -7,7 +7,7 @@ use syntax::ast::Lit_::LitBool; declare_lint! { pub NONSENSICAL_OPEN_OPTIONS, Warn, - "The options used for opening a file are nonsensical" + "nonsensical combination of options for opening a file" } @@ -42,14 +42,14 @@ enum Argument { #[derive(Debug)] enum OpenOption { - Write(Argument), - Read(Argument), - Truncate(Argument), - Create(Argument), - Append(Argument) + Write, + Read, + Truncate, + Create, + Append } -fn get_open_options(cx: &LateContext, argument: &Expr, options: &mut Vec) { +fn get_open_options(cx: &LateContext, argument: &Expr, options: &mut Vec<(OpenOption, Argument)>) { if let ExprMethodCall(ref name, _, ref arguments) = argument.node { let (obj_ty, _) = walk_ptrs_ty_depth(cx.tcx.expr_ty(&arguments[0])); @@ -73,19 +73,19 @@ fn get_open_options(cx: &LateContext, argument: &Expr, options: &mut Vec { - options.push(OpenOption::Create(argument_option)); + options.push((OpenOption::Create, argument_option)); }, "append" => { - options.push(OpenOption::Append(argument_option)); + options.push((OpenOption::Append, argument_option)); }, "truncate" => { - options.push(OpenOption::Truncate(argument_option)); + options.push((OpenOption::Truncate, argument_option)); }, "read" => { - options.push(OpenOption::Read(argument_option)); + options.push((OpenOption::Read, argument_option)); }, "write" => { - options.push(OpenOption::Write(argument_option)); + options.push((OpenOption::Write, argument_option)); }, _ => {} } @@ -95,45 +95,45 @@ fn get_open_options(cx: &LateContext, argument: &Expr, options: &mut Vec 1 { + if options.iter().filter(|o| if let (OpenOption::Create, _) = **o {true} else {false}).count() > 1 { span_lint(cx, NONSENSICAL_OPEN_OPTIONS, span, "The method \"create\" \ is called more than once"); } - if options.iter().filter(|o| if let OpenOption::Append(_) = **o {true} else {false}).count() > 1 { + if options.iter().filter(|o| if let (OpenOption::Append, _) = **o {true} else {false}).count() > 1 { span_lint(cx, NONSENSICAL_OPEN_OPTIONS, span, "The method \"append\" \ is called more than once"); } - if options.iter().filter(|o| if let OpenOption::Truncate(_) = **o {true} else {false}).count() > 1 { + if options.iter().filter(|o| if let (OpenOption::Truncate, _) = **o {true} else {false}).count() > 1 { span_lint(cx, NONSENSICAL_OPEN_OPTIONS, span, "The method \"truncate\" \ is called more than once"); } - if options.iter().filter(|o| if let OpenOption::Read(_) = **o {true} else {false}).count() > 1 { + if options.iter().filter(|o| if let (OpenOption::Read, _) = **o {true} else {false}).count() > 1 { span_lint(cx, NONSENSICAL_OPEN_OPTIONS, span, "The method \"read\" \ is called more than once"); } - if options.iter().filter(|o| if let OpenOption::Write(_) = **o {true} else {false}).count() > 1 { + if options.iter().filter(|o| if let (OpenOption::Write, _) = **o {true} else {false}).count() > 1 { span_lint(cx, NONSENSICAL_OPEN_OPTIONS, span, "The method \"write\" \ is called more than once"); } } -fn check_for_inconsistencies(cx: &LateContext, options: &[OpenOption], span: Span) { +fn check_for_inconsistencies(cx: &LateContext, options: &[(OpenOption, Argument)], span: Span) { // Truncate + read makes no sense. - if options.iter().filter(|o| if let OpenOption::Read(Argument::True) = **o {true} else {false}).count() > 0 && - options.iter().filter(|o| if let OpenOption::Truncate(Argument::True) = **o {true} else {false}).count() > 0 { + if options.iter().filter(|o| if let (OpenOption::Read, Argument::True) = **o {true} else {false}).count() > 0 && + options.iter().filter(|o| if let (OpenOption::Truncate, Argument::True) = **o {true} else {false}).count() > 0 { span_lint(cx, NONSENSICAL_OPEN_OPTIONS, span, "File opened with \"truncate\" and \"read\""); } // Append + truncate makes no sense. - if options.iter().filter(|o| if let OpenOption::Append(Argument::True) = **o {true} else {false}).count() > 0 && - options.iter().filter(|o| if let OpenOption::Truncate(Argument::True) = **o {true} else {false}).count() > 0 { + if options.iter().filter(|o| if let (OpenOption::Append, Argument::True) = **o {true} else {false}).count() > 0 && + options.iter().filter(|o| if let (OpenOption::Truncate, Argument::True) = **o {true} else {false}).count() > 0 { span_lint(cx, NONSENSICAL_OPEN_OPTIONS, span, "File opened with \"append\" and \"truncate\""); } } -fn check_open_options(cx: &LateContext, options: &[OpenOption], span: Span) { +fn check_open_options(cx: &LateContext, options: &[(OpenOption, Argument)], span: Span) { check_for_duplicates(cx, options, span); check_for_inconsistencies(cx, options, span); }