Skip to content

Commit 0cc01ce

Browse files
committed
Move OpenOptions into Methods lint pass
1 parent 508cf6b commit 0cc01ce

File tree

6 files changed

+39
-39
lines changed

6 files changed

+39
-39
lines changed

clippy_lints/src/lib.register_all.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,7 @@ store.register_group(true, "clippy::all", Some("clippy_all"), vec![
187187
LintId::of(methods::NEEDLESS_OPTION_TAKE),
188188
LintId::of(methods::NEEDLESS_SPLITN),
189189
LintId::of(methods::NEW_RET_NO_SELF),
190+
LintId::of(methods::NONSENSICAL_OPEN_OPTIONS),
190191
LintId::of(methods::NO_EFFECT_REPLACE),
191192
LintId::of(methods::OBFUSCATED_IF_ELSE),
192193
LintId::of(methods::OK_EXPECT),
@@ -246,7 +247,6 @@ store.register_group(true, "clippy::all", Some("clippy_all"), vec![
246247
LintId::of(non_expressive_names::JUST_UNDERSCORES_AND_DIGITS),
247248
LintId::of(non_octal_unix_permissions::NON_OCTAL_UNIX_PERMISSIONS),
248249
LintId::of(octal_escapes::OCTAL_ESCAPES),
249-
LintId::of(open_options::NONSENSICAL_OPEN_OPTIONS),
250250
LintId::of(operators::ABSURD_EXTREME_COMPARISONS),
251251
LintId::of(operators::ASSIGN_OP_PATTERN),
252252
LintId::of(operators::BAD_BIT_MASK),

clippy_lints/src/lib.register_correctness.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,12 @@ store.register_group(true, "clippy::correctness", Some("clippy_correctness"), ve
3939
LintId::of(mem_replace::MEM_REPLACE_WITH_UNINIT),
4040
LintId::of(methods::CLONE_DOUBLE_REF),
4141
LintId::of(methods::ITERATOR_STEP_BY_ZERO),
42+
LintId::of(methods::NONSENSICAL_OPEN_OPTIONS),
4243
LintId::of(methods::SUSPICIOUS_SPLITN),
4344
LintId::of(methods::UNINIT_ASSUMED_INIT),
4445
LintId::of(methods::ZST_OFFSET),
4546
LintId::of(minmax::MIN_MAX),
4647
LintId::of(non_octal_unix_permissions::NON_OCTAL_UNIX_PERMISSIONS),
47-
LintId::of(open_options::NONSENSICAL_OPEN_OPTIONS),
4848
LintId::of(operators::ABSURD_EXTREME_COMPARISONS),
4949
LintId::of(operators::BAD_BIT_MASK),
5050
LintId::of(operators::CMP_NAN),

clippy_lints/src/lib.register_lints.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,7 @@ store.register_lints(&[
335335
methods::NEEDLESS_OPTION_TAKE,
336336
methods::NEEDLESS_SPLITN,
337337
methods::NEW_RET_NO_SELF,
338+
methods::NONSENSICAL_OPEN_OPTIONS,
338339
methods::NO_EFFECT_REPLACE,
339340
methods::OBFUSCATED_IF_ELSE,
340341
methods::OK_EXPECT,
@@ -421,7 +422,6 @@ store.register_lints(&[
421422
nonstandard_macro_braces::NONSTANDARD_MACRO_BRACES,
422423
octal_escapes::OCTAL_ESCAPES,
423424
only_used_in_recursion::ONLY_USED_IN_RECURSION,
424-
open_options::NONSENSICAL_OPEN_OPTIONS,
425425
operators::ABSURD_EXTREME_COMPARISONS,
426426
operators::ARITHMETIC,
427427
operators::ASSIGN_OP_PATTERN,

clippy_lints/src/lib.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,6 @@ mod non_send_fields_in_send_ty;
315315
mod nonstandard_macro_braces;
316316
mod octal_escapes;
317317
mod only_used_in_recursion;
318-
mod open_options;
319318
mod operators;
320319
mod option_env_unwrap;
321320
mod option_if_let_else;
@@ -641,7 +640,6 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
641640
store.register_late_pass(|| Box::new(lifetimes::Lifetimes));
642641
store.register_late_pass(|| Box::new(entry::HashMapPass));
643642
store.register_late_pass(|| Box::new(minmax::MinMaxPass));
644-
store.register_late_pass(|| Box::new(open_options::OpenOptions));
645643
store.register_late_pass(|| Box::new(zero_div_zero::ZeroDiv));
646644
store.register_late_pass(|| Box::new(mutex_atomic::Mutex));
647645
store.register_late_pass(|| Box::new(needless_update::NeedlessUpdate));

clippy_lints/src/methods/mod.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ mod needless_option_take;
5757
mod no_effect_replace;
5858
mod obfuscated_if_else;
5959
mod ok_expect;
60+
mod open_options;
6061
mod option_as_ref_deref;
6162
mod option_map_or_none;
6263
mod option_map_unwrap_or;
@@ -2679,6 +2680,27 @@ declare_clippy_lint! {
26792680
"`&mut Mutex::lock` does unnecessary locking"
26802681
}
26812682

2683+
declare_clippy_lint! {
2684+
/// ### What it does
2685+
/// Checks for duplicate open options as well as combinations
2686+
/// that make no sense.
2687+
///
2688+
/// ### Why is this bad?
2689+
/// In the best case, the code will be harder to read than
2690+
/// necessary. I don't know the worst case.
2691+
///
2692+
/// ### Example
2693+
/// ```rust
2694+
/// use std::fs::OpenOptions;
2695+
///
2696+
/// OpenOptions::new().read(true).truncate(true);
2697+
/// ```
2698+
#[clippy::version = "pre 1.29.0"]
2699+
pub NONSENSICAL_OPEN_OPTIONS,
2700+
correctness,
2701+
"nonsensical combination of options for opening a file"
2702+
}
2703+
26822704
pub struct Methods {
26832705
avoid_breaking_exported_api: bool,
26842706
msrv: Option<RustcVersion>,
@@ -2791,6 +2813,7 @@ impl_lint_pass!(Methods => [
27912813
MAP_CLONE,
27922814
MAP_ERR_IGNORE,
27932815
MUT_MUTEX_LOCK,
2816+
NONSENSICAL_OPEN_OPTIONS,
27942817
]);
27952818

27962819
/// Extracts a method call name, args, and `Span` of the method name.
@@ -3168,6 +3191,9 @@ impl Methods {
31683191
_ => iter_nth_zero::check(cx, expr, recv, n_arg),
31693192
},
31703193
("ok_or_else", [arg]) => unnecessary_lazy_eval::check(cx, expr, recv, arg, "ok_or"),
3194+
("open", [_]) => {
3195+
open_options::check(cx, expr, recv);
3196+
},
31713197
("or_else", [arg]) => {
31723198
if !bind_instead_of_map::ResultOrElseErrInfo::check(cx, expr, recv, arg) {
31733199
unnecessary_lazy_eval::check(cx, expr, recv, arg, "or");

clippy_lints/src/open_options.rs renamed to clippy_lints/src/methods/open_options.rs

Lines changed: 10 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -3,43 +3,19 @@ use clippy_utils::paths;
33
use clippy_utils::ty::match_type;
44
use rustc_ast::ast::LitKind;
55
use rustc_hir::{Expr, ExprKind};
6-
use rustc_lint::{LateContext, LateLintPass};
7-
use rustc_session::{declare_lint_pass, declare_tool_lint};
6+
use rustc_lint::LateContext;
87
use rustc_span::source_map::{Span, Spanned};
98

10-
declare_clippy_lint! {
11-
/// ### What it does
12-
/// Checks for duplicate open options as well as combinations
13-
/// that make no sense.
14-
///
15-
/// ### Why is this bad?
16-
/// In the best case, the code will be harder to read than
17-
/// necessary. I don't know the worst case.
18-
///
19-
/// ### Example
20-
/// ```rust
21-
/// use std::fs::OpenOptions;
22-
///
23-
/// OpenOptions::new().read(true).truncate(true);
24-
/// ```
25-
#[clippy::version = "pre 1.29.0"]
26-
pub NONSENSICAL_OPEN_OPTIONS,
27-
correctness,
28-
"nonsensical combination of options for opening a file"
29-
}
30-
31-
declare_lint_pass!(OpenOptions => [NONSENSICAL_OPEN_OPTIONS]);
9+
use super::NONSENSICAL_OPEN_OPTIONS;
3210

33-
impl<'tcx> LateLintPass<'tcx> for OpenOptions {
34-
fn check_expr(&mut self, cx: &LateContext<'tcx>, e: &'tcx Expr<'_>) {
35-
if let ExprKind::MethodCall(path, [self_arg, ..], _) = &e.kind {
36-
let obj_ty = cx.typeck_results().expr_ty(self_arg).peel_refs();
37-
if path.ident.name == sym!(open) && match_type(cx, obj_ty, &paths::OPEN_OPTIONS) {
38-
let mut options = Vec::new();
39-
get_open_options(cx, self_arg, &mut options);
40-
check_open_options(cx, &options, e.span);
41-
}
42-
}
11+
pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, e: &'tcx Expr<'_>, recv: &'tcx Expr<'_>) {
12+
if let Some(method_id) = cx.typeck_results().type_dependent_def_id(e.hir_id)
13+
&& let Some(impl_id) = cx.tcx.impl_of_method(method_id)
14+
&& match_type(cx, cx.tcx.type_of(impl_id), &paths::OPEN_OPTIONS)
15+
{
16+
let mut options = Vec::new();
17+
get_open_options(cx, recv, &mut options);
18+
check_open_options(cx, &options, e.span);
4319
}
4420
}
4521

0 commit comments

Comments
 (0)