Move OpenOptions into Methods lint pass
This commit is contained in:
@@ -187,6 +187,7 @@ store.register_group(true, "clippy::all", Some("clippy_all"), vec![
|
|||||||
LintId::of(methods::NEEDLESS_OPTION_TAKE),
|
LintId::of(methods::NEEDLESS_OPTION_TAKE),
|
||||||
LintId::of(methods::NEEDLESS_SPLITN),
|
LintId::of(methods::NEEDLESS_SPLITN),
|
||||||
LintId::of(methods::NEW_RET_NO_SELF),
|
LintId::of(methods::NEW_RET_NO_SELF),
|
||||||
|
LintId::of(methods::NONSENSICAL_OPEN_OPTIONS),
|
||||||
LintId::of(methods::NO_EFFECT_REPLACE),
|
LintId::of(methods::NO_EFFECT_REPLACE),
|
||||||
LintId::of(methods::OBFUSCATED_IF_ELSE),
|
LintId::of(methods::OBFUSCATED_IF_ELSE),
|
||||||
LintId::of(methods::OK_EXPECT),
|
LintId::of(methods::OK_EXPECT),
|
||||||
@@ -246,7 +247,6 @@ store.register_group(true, "clippy::all", Some("clippy_all"), vec![
|
|||||||
LintId::of(non_expressive_names::JUST_UNDERSCORES_AND_DIGITS),
|
LintId::of(non_expressive_names::JUST_UNDERSCORES_AND_DIGITS),
|
||||||
LintId::of(non_octal_unix_permissions::NON_OCTAL_UNIX_PERMISSIONS),
|
LintId::of(non_octal_unix_permissions::NON_OCTAL_UNIX_PERMISSIONS),
|
||||||
LintId::of(octal_escapes::OCTAL_ESCAPES),
|
LintId::of(octal_escapes::OCTAL_ESCAPES),
|
||||||
LintId::of(open_options::NONSENSICAL_OPEN_OPTIONS),
|
|
||||||
LintId::of(operators::ABSURD_EXTREME_COMPARISONS),
|
LintId::of(operators::ABSURD_EXTREME_COMPARISONS),
|
||||||
LintId::of(operators::ASSIGN_OP_PATTERN),
|
LintId::of(operators::ASSIGN_OP_PATTERN),
|
||||||
LintId::of(operators::BAD_BIT_MASK),
|
LintId::of(operators::BAD_BIT_MASK),
|
||||||
|
|||||||
@@ -39,12 +39,12 @@ store.register_group(true, "clippy::correctness", Some("clippy_correctness"), ve
|
|||||||
LintId::of(mem_replace::MEM_REPLACE_WITH_UNINIT),
|
LintId::of(mem_replace::MEM_REPLACE_WITH_UNINIT),
|
||||||
LintId::of(methods::CLONE_DOUBLE_REF),
|
LintId::of(methods::CLONE_DOUBLE_REF),
|
||||||
LintId::of(methods::ITERATOR_STEP_BY_ZERO),
|
LintId::of(methods::ITERATOR_STEP_BY_ZERO),
|
||||||
|
LintId::of(methods::NONSENSICAL_OPEN_OPTIONS),
|
||||||
LintId::of(methods::SUSPICIOUS_SPLITN),
|
LintId::of(methods::SUSPICIOUS_SPLITN),
|
||||||
LintId::of(methods::UNINIT_ASSUMED_INIT),
|
LintId::of(methods::UNINIT_ASSUMED_INIT),
|
||||||
LintId::of(methods::ZST_OFFSET),
|
LintId::of(methods::ZST_OFFSET),
|
||||||
LintId::of(minmax::MIN_MAX),
|
LintId::of(minmax::MIN_MAX),
|
||||||
LintId::of(non_octal_unix_permissions::NON_OCTAL_UNIX_PERMISSIONS),
|
LintId::of(non_octal_unix_permissions::NON_OCTAL_UNIX_PERMISSIONS),
|
||||||
LintId::of(open_options::NONSENSICAL_OPEN_OPTIONS),
|
|
||||||
LintId::of(operators::ABSURD_EXTREME_COMPARISONS),
|
LintId::of(operators::ABSURD_EXTREME_COMPARISONS),
|
||||||
LintId::of(operators::BAD_BIT_MASK),
|
LintId::of(operators::BAD_BIT_MASK),
|
||||||
LintId::of(operators::CMP_NAN),
|
LintId::of(operators::CMP_NAN),
|
||||||
|
|||||||
@@ -335,6 +335,7 @@ store.register_lints(&[
|
|||||||
methods::NEEDLESS_OPTION_TAKE,
|
methods::NEEDLESS_OPTION_TAKE,
|
||||||
methods::NEEDLESS_SPLITN,
|
methods::NEEDLESS_SPLITN,
|
||||||
methods::NEW_RET_NO_SELF,
|
methods::NEW_RET_NO_SELF,
|
||||||
|
methods::NONSENSICAL_OPEN_OPTIONS,
|
||||||
methods::NO_EFFECT_REPLACE,
|
methods::NO_EFFECT_REPLACE,
|
||||||
methods::OBFUSCATED_IF_ELSE,
|
methods::OBFUSCATED_IF_ELSE,
|
||||||
methods::OK_EXPECT,
|
methods::OK_EXPECT,
|
||||||
@@ -421,7 +422,6 @@ store.register_lints(&[
|
|||||||
nonstandard_macro_braces::NONSTANDARD_MACRO_BRACES,
|
nonstandard_macro_braces::NONSTANDARD_MACRO_BRACES,
|
||||||
octal_escapes::OCTAL_ESCAPES,
|
octal_escapes::OCTAL_ESCAPES,
|
||||||
only_used_in_recursion::ONLY_USED_IN_RECURSION,
|
only_used_in_recursion::ONLY_USED_IN_RECURSION,
|
||||||
open_options::NONSENSICAL_OPEN_OPTIONS,
|
|
||||||
operators::ABSURD_EXTREME_COMPARISONS,
|
operators::ABSURD_EXTREME_COMPARISONS,
|
||||||
operators::ARITHMETIC,
|
operators::ARITHMETIC,
|
||||||
operators::ASSIGN_OP_PATTERN,
|
operators::ASSIGN_OP_PATTERN,
|
||||||
|
|||||||
@@ -315,7 +315,6 @@ mod non_send_fields_in_send_ty;
|
|||||||
mod nonstandard_macro_braces;
|
mod nonstandard_macro_braces;
|
||||||
mod octal_escapes;
|
mod octal_escapes;
|
||||||
mod only_used_in_recursion;
|
mod only_used_in_recursion;
|
||||||
mod open_options;
|
|
||||||
mod operators;
|
mod operators;
|
||||||
mod option_env_unwrap;
|
mod option_env_unwrap;
|
||||||
mod option_if_let_else;
|
mod option_if_let_else;
|
||||||
@@ -641,7 +640,6 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
|
|||||||
store.register_late_pass(|| Box::new(lifetimes::Lifetimes));
|
store.register_late_pass(|| Box::new(lifetimes::Lifetimes));
|
||||||
store.register_late_pass(|| Box::new(entry::HashMapPass));
|
store.register_late_pass(|| Box::new(entry::HashMapPass));
|
||||||
store.register_late_pass(|| Box::new(minmax::MinMaxPass));
|
store.register_late_pass(|| Box::new(minmax::MinMaxPass));
|
||||||
store.register_late_pass(|| Box::new(open_options::OpenOptions));
|
|
||||||
store.register_late_pass(|| Box::new(zero_div_zero::ZeroDiv));
|
store.register_late_pass(|| Box::new(zero_div_zero::ZeroDiv));
|
||||||
store.register_late_pass(|| Box::new(mutex_atomic::Mutex));
|
store.register_late_pass(|| Box::new(mutex_atomic::Mutex));
|
||||||
store.register_late_pass(|| Box::new(needless_update::NeedlessUpdate));
|
store.register_late_pass(|| Box::new(needless_update::NeedlessUpdate));
|
||||||
|
|||||||
@@ -57,6 +57,7 @@ mod needless_option_take;
|
|||||||
mod no_effect_replace;
|
mod no_effect_replace;
|
||||||
mod obfuscated_if_else;
|
mod obfuscated_if_else;
|
||||||
mod ok_expect;
|
mod ok_expect;
|
||||||
|
mod open_options;
|
||||||
mod option_as_ref_deref;
|
mod option_as_ref_deref;
|
||||||
mod option_map_or_none;
|
mod option_map_or_none;
|
||||||
mod option_map_unwrap_or;
|
mod option_map_unwrap_or;
|
||||||
@@ -2679,6 +2680,27 @@ declare_clippy_lint! {
|
|||||||
"`&mut Mutex::lock` does unnecessary locking"
|
"`&mut Mutex::lock` does unnecessary locking"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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.
|
||||||
|
///
|
||||||
|
/// ### Example
|
||||||
|
/// ```rust
|
||||||
|
/// use std::fs::OpenOptions;
|
||||||
|
///
|
||||||
|
/// OpenOptions::new().read(true).truncate(true);
|
||||||
|
/// ```
|
||||||
|
#[clippy::version = "pre 1.29.0"]
|
||||||
|
pub NONSENSICAL_OPEN_OPTIONS,
|
||||||
|
correctness,
|
||||||
|
"nonsensical combination of options for opening a file"
|
||||||
|
}
|
||||||
|
|
||||||
pub struct Methods {
|
pub struct Methods {
|
||||||
avoid_breaking_exported_api: bool,
|
avoid_breaking_exported_api: bool,
|
||||||
msrv: Option<RustcVersion>,
|
msrv: Option<RustcVersion>,
|
||||||
@@ -2791,6 +2813,7 @@ impl_lint_pass!(Methods => [
|
|||||||
MAP_CLONE,
|
MAP_CLONE,
|
||||||
MAP_ERR_IGNORE,
|
MAP_ERR_IGNORE,
|
||||||
MUT_MUTEX_LOCK,
|
MUT_MUTEX_LOCK,
|
||||||
|
NONSENSICAL_OPEN_OPTIONS,
|
||||||
]);
|
]);
|
||||||
|
|
||||||
/// Extracts a method call name, args, and `Span` of the method name.
|
/// Extracts a method call name, args, and `Span` of the method name.
|
||||||
@@ -3168,6 +3191,9 @@ impl Methods {
|
|||||||
_ => iter_nth_zero::check(cx, expr, recv, n_arg),
|
_ => iter_nth_zero::check(cx, expr, recv, n_arg),
|
||||||
},
|
},
|
||||||
("ok_or_else", [arg]) => unnecessary_lazy_eval::check(cx, expr, recv, arg, "ok_or"),
|
("ok_or_else", [arg]) => unnecessary_lazy_eval::check(cx, expr, recv, arg, "ok_or"),
|
||||||
|
("open", [_]) => {
|
||||||
|
open_options::check(cx, expr, recv);
|
||||||
|
},
|
||||||
("or_else", [arg]) => {
|
("or_else", [arg]) => {
|
||||||
if !bind_instead_of_map::ResultOrElseErrInfo::check(cx, expr, recv, arg) {
|
if !bind_instead_of_map::ResultOrElseErrInfo::check(cx, expr, recv, arg) {
|
||||||
unnecessary_lazy_eval::check(cx, expr, recv, arg, "or");
|
unnecessary_lazy_eval::check(cx, expr, recv, arg, "or");
|
||||||
|
|||||||
@@ -3,44 +3,20 @@ use clippy_utils::paths;
|
|||||||
use clippy_utils::ty::match_type;
|
use clippy_utils::ty::match_type;
|
||||||
use rustc_ast::ast::LitKind;
|
use rustc_ast::ast::LitKind;
|
||||||
use rustc_hir::{Expr, ExprKind};
|
use rustc_hir::{Expr, ExprKind};
|
||||||
use rustc_lint::{LateContext, LateLintPass};
|
use rustc_lint::LateContext;
|
||||||
use rustc_session::{declare_lint_pass, declare_tool_lint};
|
|
||||||
use rustc_span::source_map::{Span, Spanned};
|
use rustc_span::source_map::{Span, Spanned};
|
||||||
|
|
||||||
declare_clippy_lint! {
|
use super::NONSENSICAL_OPEN_OPTIONS;
|
||||||
/// ### 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.
|
|
||||||
///
|
|
||||||
/// ### Example
|
|
||||||
/// ```rust
|
|
||||||
/// use std::fs::OpenOptions;
|
|
||||||
///
|
|
||||||
/// OpenOptions::new().read(true).truncate(true);
|
|
||||||
/// ```
|
|
||||||
#[clippy::version = "pre 1.29.0"]
|
|
||||||
pub NONSENSICAL_OPEN_OPTIONS,
|
|
||||||
correctness,
|
|
||||||
"nonsensical combination of options for opening a file"
|
|
||||||
}
|
|
||||||
|
|
||||||
declare_lint_pass!(OpenOptions => [NONSENSICAL_OPEN_OPTIONS]);
|
pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, e: &'tcx Expr<'_>, recv: &'tcx Expr<'_>) {
|
||||||
|
if let Some(method_id) = cx.typeck_results().type_dependent_def_id(e.hir_id)
|
||||||
impl<'tcx> LateLintPass<'tcx> for OpenOptions {
|
&& let Some(impl_id) = cx.tcx.impl_of_method(method_id)
|
||||||
fn check_expr(&mut self, cx: &LateContext<'tcx>, e: &'tcx Expr<'_>) {
|
&& match_type(cx, cx.tcx.type_of(impl_id), &paths::OPEN_OPTIONS)
|
||||||
if let ExprKind::MethodCall(path, [self_arg, ..], _) = &e.kind {
|
{
|
||||||
let obj_ty = cx.typeck_results().expr_ty(self_arg).peel_refs();
|
|
||||||
if path.ident.name == sym!(open) && match_type(cx, obj_ty, &paths::OPEN_OPTIONS) {
|
|
||||||
let mut options = Vec::new();
|
let mut options = Vec::new();
|
||||||
get_open_options(cx, self_arg, &mut options);
|
get_open_options(cx, recv, &mut options);
|
||||||
check_open_options(cx, &options, e.span);
|
check_open_options(cx, &options, e.span);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
|
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
|
||||||
Reference in New Issue
Block a user