Skip to content

Commit dded6ed

Browse files
committed
f
1 parent 11d7080 commit dded6ed

File tree

10 files changed

+275
-241
lines changed

10 files changed

+275
-241
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ path = "src/driver.rs"
2323
[dependencies]
2424
clippy_config = { path = "clippy_config" }
2525
clippy_lints = { path = "clippy_lints" }
26+
clippy_lints_methods = { path = "clippy_lints_methods" }
2627
clippy_utils = { path = "clippy_utils" }
2728
declare_clippy_lint = { path = "declare_clippy_lint" }
2829
rustc_tools_util = { path = "rustc_tools_util", version = "0.4.2" }

clippy_dev/src/update_lints.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ macro_rules! declare_lint_crates {
4949
}
5050
declare_lint_crates! {
5151
clippy_lints,
52+
clippy_lints_methods,
5253
}
5354

5455
/// Runs the `update_lints` command.
@@ -196,7 +197,7 @@ pub fn generate_lint_files(
196197
|dst| {
197198
for lint_mod in lints
198199
.iter()
199-
.filter(|l| l.def_crate == c)
200+
.filter(|l| l.def_crate == c && !l.module.is_empty())
200201
.map(|l| l.module.split_once("::").map_or(&*l.module, |x| x.0))
201202
.sorted()
202203
.dedup()
@@ -213,13 +214,17 @@ pub fn generate_lint_files(
213214
&mut |_, src, dst| {
214215
dst.push_str(GENERATED_FILE_COMMENT);
215216
dst.push_str("pub static LINTS: &[&::declare_clippy_lint::LintInfo] = &[\n");
216-
for (module_name, lint_name) in lints
217+
for (module, lint_name) in lints
217218
.iter()
218219
.filter(|l| l.def_crate == c)
219220
.map(|l| (&l.module, l.name.to_uppercase()))
220221
.sorted()
221222
{
222-
writeln!(dst, " crate::{module_name}::{lint_name}_INFO,").unwrap();
223+
if module.is_empty() {
224+
writeln!(dst, " crate::{lint_name}_INFO,").unwrap();
225+
} else {
226+
writeln!(dst, " crate::{module}::{lint_name}_INFO,").unwrap();
227+
}
223228
}
224229
dst.push_str("];\n");
225230
UpdateStatus::from_changed(src != dst)
@@ -284,7 +289,7 @@ fn read_src_with_module(src_root: &Path) -> impl use<'_> + Iterator<Item = (DirE
284289
};
285290
let path = e.path().as_os_str().as_encoded_bytes();
286291
if let Some(path) = path.strip_suffix(b".rs")
287-
&& let Some(path) = path.get("clippy_lints/src/".len()..)
292+
&& let Some(path) = path.get(src_root.as_os_str().len() + 1..)
288293
{
289294
if path == b"lib" {
290295
Some((e, String::new()))

clippy_lints/src/declared_lints.rs

Lines changed: 0 additions & 147 deletions
Original file line numberDiff line numberDiff line change
@@ -344,153 +344,6 @@ pub static LINTS: &[&::declare_clippy_lint::LintInfo] = &[
344344
crate::mem_replace::MEM_REPLACE_OPTION_WITH_SOME_INFO,
345345
crate::mem_replace::MEM_REPLACE_WITH_DEFAULT_INFO,
346346
crate::mem_replace::MEM_REPLACE_WITH_UNINIT_INFO,
347-
crate::methods::BIND_INSTEAD_OF_MAP_INFO,
348-
crate::methods::BYTES_COUNT_TO_LEN_INFO,
349-
crate::methods::BYTES_NTH_INFO,
350-
crate::methods::CASE_SENSITIVE_FILE_EXTENSION_COMPARISONS_INFO,
351-
crate::methods::CHARS_LAST_CMP_INFO,
352-
crate::methods::CHARS_NEXT_CMP_INFO,
353-
crate::methods::CLEAR_WITH_DRAIN_INFO,
354-
crate::methods::CLONED_INSTEAD_OF_COPIED_INFO,
355-
crate::methods::CLONE_ON_COPY_INFO,
356-
crate::methods::CLONE_ON_REF_PTR_INFO,
357-
crate::methods::COLLAPSIBLE_STR_REPLACE_INFO,
358-
crate::methods::CONST_IS_EMPTY_INFO,
359-
crate::methods::DOUBLE_ENDED_ITERATOR_LAST_INFO,
360-
crate::methods::DRAIN_COLLECT_INFO,
361-
crate::methods::ERR_EXPECT_INFO,
362-
crate::methods::EXPECT_FUN_CALL_INFO,
363-
crate::methods::EXPECT_USED_INFO,
364-
crate::methods::EXTEND_WITH_DRAIN_INFO,
365-
crate::methods::FILETYPE_IS_FILE_INFO,
366-
crate::methods::FILTER_MAP_BOOL_THEN_INFO,
367-
crate::methods::FILTER_MAP_IDENTITY_INFO,
368-
crate::methods::FILTER_MAP_NEXT_INFO,
369-
crate::methods::FILTER_NEXT_INFO,
370-
crate::methods::FLAT_MAP_IDENTITY_INFO,
371-
crate::methods::FLAT_MAP_OPTION_INFO,
372-
crate::methods::FORMAT_COLLECT_INFO,
373-
crate::methods::FROM_ITER_INSTEAD_OF_COLLECT_INFO,
374-
crate::methods::GET_FIRST_INFO,
375-
crate::methods::GET_LAST_WITH_LEN_INFO,
376-
crate::methods::GET_UNWRAP_INFO,
377-
crate::methods::IMPLICIT_CLONE_INFO,
378-
crate::methods::INEFFICIENT_TO_STRING_INFO,
379-
crate::methods::INSPECT_FOR_EACH_INFO,
380-
crate::methods::INTO_ITER_ON_REF_INFO,
381-
crate::methods::IO_OTHER_ERROR_INFO,
382-
crate::methods::IS_DIGIT_ASCII_RADIX_INFO,
383-
crate::methods::ITERATOR_STEP_BY_ZERO_INFO,
384-
crate::methods::ITER_CLONED_COLLECT_INFO,
385-
crate::methods::ITER_COUNT_INFO,
386-
crate::methods::ITER_FILTER_IS_OK_INFO,
387-
crate::methods::ITER_FILTER_IS_SOME_INFO,
388-
crate::methods::ITER_KV_MAP_INFO,
389-
crate::methods::ITER_NEXT_SLICE_INFO,
390-
crate::methods::ITER_NTH_INFO,
391-
crate::methods::ITER_NTH_ZERO_INFO,
392-
crate::methods::ITER_ON_EMPTY_COLLECTIONS_INFO,
393-
crate::methods::ITER_ON_SINGLE_ITEMS_INFO,
394-
crate::methods::ITER_OUT_OF_BOUNDS_INFO,
395-
crate::methods::ITER_OVEREAGER_CLONED_INFO,
396-
crate::methods::ITER_SKIP_NEXT_INFO,
397-
crate::methods::ITER_SKIP_ZERO_INFO,
398-
crate::methods::ITER_WITH_DRAIN_INFO,
399-
crate::methods::JOIN_ABSOLUTE_PATHS_INFO,
400-
crate::methods::MANUAL_CONTAINS_INFO,
401-
crate::methods::MANUAL_C_STR_LITERALS_INFO,
402-
crate::methods::MANUAL_FILTER_MAP_INFO,
403-
crate::methods::MANUAL_FIND_MAP_INFO,
404-
crate::methods::MANUAL_INSPECT_INFO,
405-
crate::methods::MANUAL_IS_VARIANT_AND_INFO,
406-
crate::methods::MANUAL_NEXT_BACK_INFO,
407-
crate::methods::MANUAL_OK_OR_INFO,
408-
crate::methods::MANUAL_REPEAT_N_INFO,
409-
crate::methods::MANUAL_SATURATING_ARITHMETIC_INFO,
410-
crate::methods::MANUAL_SPLIT_ONCE_INFO,
411-
crate::methods::MANUAL_STR_REPEAT_INFO,
412-
crate::methods::MANUAL_TRY_FOLD_INFO,
413-
crate::methods::MAP_ALL_ANY_IDENTITY_INFO,
414-
crate::methods::MAP_CLONE_INFO,
415-
crate::methods::MAP_COLLECT_RESULT_UNIT_INFO,
416-
crate::methods::MAP_ERR_IGNORE_INFO,
417-
crate::methods::MAP_FLATTEN_INFO,
418-
crate::methods::MAP_IDENTITY_INFO,
419-
crate::methods::MAP_UNWRAP_OR_INFO,
420-
crate::methods::MAP_WITH_UNUSED_ARGUMENT_OVER_RANGES_INFO,
421-
crate::methods::MUT_MUTEX_LOCK_INFO,
422-
crate::methods::NAIVE_BYTECOUNT_INFO,
423-
crate::methods::NEEDLESS_AS_BYTES_INFO,
424-
crate::methods::NEEDLESS_CHARACTER_ITERATION_INFO,
425-
crate::methods::NEEDLESS_COLLECT_INFO,
426-
crate::methods::NEEDLESS_OPTION_AS_DEREF_INFO,
427-
crate::methods::NEEDLESS_OPTION_TAKE_INFO,
428-
crate::methods::NEEDLESS_SPLITN_INFO,
429-
crate::methods::NEW_RET_NO_SELF_INFO,
430-
crate::methods::NONSENSICAL_OPEN_OPTIONS_INFO,
431-
crate::methods::NO_EFFECT_REPLACE_INFO,
432-
crate::methods::OBFUSCATED_IF_ELSE_INFO,
433-
crate::methods::OK_EXPECT_INFO,
434-
crate::methods::OPTION_AS_REF_CLONED_INFO,
435-
crate::methods::OPTION_AS_REF_DEREF_INFO,
436-
crate::methods::OPTION_FILTER_MAP_INFO,
437-
crate::methods::OPTION_MAP_OR_NONE_INFO,
438-
crate::methods::OR_FUN_CALL_INFO,
439-
crate::methods::OR_THEN_UNWRAP_INFO,
440-
crate::methods::PATH_BUF_PUSH_OVERWRITE_INFO,
441-
crate::methods::PATH_ENDS_WITH_EXT_INFO,
442-
crate::methods::RANGE_ZIP_WITH_LEN_INFO,
443-
crate::methods::READONLY_WRITE_LOCK_INFO,
444-
crate::methods::READ_LINE_WITHOUT_TRIM_INFO,
445-
crate::methods::REDUNDANT_AS_STR_INFO,
446-
crate::methods::REPEAT_ONCE_INFO,
447-
crate::methods::RESULT_FILTER_MAP_INFO,
448-
crate::methods::RESULT_MAP_OR_INTO_OPTION_INFO,
449-
crate::methods::RETURN_AND_THEN_INFO,
450-
crate::methods::SEARCH_IS_SOME_INFO,
451-
crate::methods::SEEK_FROM_CURRENT_INFO,
452-
crate::methods::SEEK_TO_START_INSTEAD_OF_REWIND_INFO,
453-
crate::methods::SHOULD_IMPLEMENT_TRAIT_INFO,
454-
crate::methods::SINGLE_CHAR_ADD_STR_INFO,
455-
crate::methods::SKIP_WHILE_NEXT_INFO,
456-
crate::methods::SLICED_STRING_AS_BYTES_INFO,
457-
crate::methods::STABLE_SORT_PRIMITIVE_INFO,
458-
crate::methods::STRING_EXTEND_CHARS_INFO,
459-
crate::methods::STRING_LIT_CHARS_ANY_INFO,
460-
crate::methods::STR_SPLIT_AT_NEWLINE_INFO,
461-
crate::methods::SUSPICIOUS_COMMAND_ARG_SPACE_INFO,
462-
crate::methods::SUSPICIOUS_MAP_INFO,
463-
crate::methods::SUSPICIOUS_OPEN_OPTIONS_INFO,
464-
crate::methods::SUSPICIOUS_SPLITN_INFO,
465-
crate::methods::SUSPICIOUS_TO_OWNED_INFO,
466-
crate::methods::SWAP_WITH_TEMPORARY_INFO,
467-
crate::methods::TYPE_ID_ON_BOX_INFO,
468-
crate::methods::UNBUFFERED_BYTES_INFO,
469-
crate::methods::UNINIT_ASSUMED_INIT_INFO,
470-
crate::methods::UNIT_HASH_INFO,
471-
crate::methods::UNNECESSARY_FALLIBLE_CONVERSIONS_INFO,
472-
crate::methods::UNNECESSARY_FILTER_MAP_INFO,
473-
crate::methods::UNNECESSARY_FIND_MAP_INFO,
474-
crate::methods::UNNECESSARY_FIRST_THEN_CHECK_INFO,
475-
crate::methods::UNNECESSARY_FOLD_INFO,
476-
crate::methods::UNNECESSARY_GET_THEN_CHECK_INFO,
477-
crate::methods::UNNECESSARY_JOIN_INFO,
478-
crate::methods::UNNECESSARY_LAZY_EVALUATIONS_INFO,
479-
crate::methods::UNNECESSARY_LITERAL_UNWRAP_INFO,
480-
crate::methods::UNNECESSARY_MAP_OR_INFO,
481-
crate::methods::UNNECESSARY_MIN_OR_MAX_INFO,
482-
crate::methods::UNNECESSARY_RESULT_MAP_OR_ELSE_INFO,
483-
crate::methods::UNNECESSARY_SORT_BY_INFO,
484-
crate::methods::UNNECESSARY_TO_OWNED_INFO,
485-
crate::methods::UNWRAP_OR_DEFAULT_INFO,
486-
crate::methods::UNWRAP_USED_INFO,
487-
crate::methods::USELESS_ASREF_INFO,
488-
crate::methods::USELESS_NONZERO_NEW_UNCHECKED_INFO,
489-
crate::methods::VEC_RESIZE_TO_ZERO_INFO,
490-
crate::methods::VERBOSE_FILE_READS_INFO,
491-
crate::methods::WAKER_CLONE_WAKE_INFO,
492-
crate::methods::WRONG_SELF_CONVENTION_INFO,
493-
crate::methods::ZST_OFFSET_INFO,
494347
crate::min_ident_chars::MIN_IDENT_CHARS_INFO,
495348
crate::minmax::MIN_MAX_INFO,
496349
crate::misc::SHORT_CIRCUIT_STATEMENT_INFO,

clippy_lints/src/ineffective_open_options.rs

Lines changed: 33 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
1-
use super::method_call;
21
use clippy_utils::diagnostics::span_lint_and_sugg;
3-
use clippy_utils::peel_blocks;
2+
use clippy_utils::{peel_blocks, sym};
43
use declare_clippy_lint::declare_clippy_lint;
54
use rustc_ast::LitKind;
65
use rustc_errors::Applicability;
76
use rustc_hir::{Expr, ExprKind};
87
use rustc_lint::{LateContext, LateLintPass};
98
use rustc_middle::ty;
109
use rustc_session::declare_lint_pass;
11-
use rustc_span::{BytePos, Span, sym};
10+
use rustc_span::{BytePos, Span};
1211

1312
declare_clippy_lint! {
1413
/// ### What it does
@@ -58,39 +57,39 @@ fn index_if_arg_is_boolean(args: &[Expr<'_>], call_span: Span) -> Option<Span> {
5857

5958
impl<'tcx> LateLintPass<'tcx> for IneffectiveOpenOptions {
6059
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
61-
let Some(("open", mut receiver, [_arg], _, _)) = method_call(expr) else {
62-
return;
63-
};
64-
let receiver_ty = cx.typeck_results().expr_ty(receiver);
65-
match receiver_ty.peel_refs().kind() {
66-
ty::Adt(adt, _) if cx.tcx.is_diagnostic_item(sym::FsOpenOptions, adt.did()) => {},
67-
_ => return,
68-
}
69-
70-
let mut append = None;
71-
let mut write = None;
72-
73-
while let Some((name, recv, args, _, span)) = method_call(receiver) {
74-
if name == "append" {
75-
append = index_if_arg_is_boolean(args, span);
76-
} else if name == "write" {
77-
write = index_if_arg_is_boolean(args, span);
60+
if let ExprKind::MethodCall(path, mut recv, [_], ..) = expr.kind
61+
&& path.ident.name == sym::open
62+
&& !expr.span.from_expansion()
63+
&& let recv_ty = cx.typeck_results().expr_ty(recv)
64+
&& let ty::Adt(adt, _) = *recv_ty.peel_refs().kind()
65+
&& cx.tcx.is_diagnostic_item(sym::FsOpenOptions, adt.did())
66+
{
67+
let mut append = None;
68+
let mut write = None;
69+
while let ExprKind::MethodCall(path, next_recv, args, span) = recv.kind
70+
&& !recv.span.from_expansion()
71+
{
72+
match path.ident.name {
73+
sym::append => append = index_if_arg_is_boolean(args, span),
74+
sym::write => write = index_if_arg_is_boolean(args, span),
75+
_ => {},
76+
}
77+
recv = next_recv;
7878
}
79-
receiver = recv;
80-
}
8179

82-
if let Some(write_span) = write
83-
&& append.is_some()
84-
{
85-
span_lint_and_sugg(
86-
cx,
87-
INEFFECTIVE_OPEN_OPTIONS,
88-
write_span,
89-
"unnecessary use of `.write(true)` because there is `.append(true)`",
90-
"remove `.write(true)`",
91-
String::new(),
92-
Applicability::MachineApplicable,
93-
);
80+
if let Some(write_span) = write
81+
&& append.is_some()
82+
{
83+
span_lint_and_sugg(
84+
cx,
85+
INEFFECTIVE_OPEN_OPTIONS,
86+
write_span,
87+
"unnecessary use of `.write(true)` because there is `.append(true)`",
88+
"remove `.write(true)`",
89+
String::new(),
90+
Applicability::MachineApplicable,
91+
);
92+
}
9493
}
9594
}
9695
}

clippy_lints/src/lib.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,6 @@ mod map_unit_fn;
226226
mod match_result_ok;
227227
mod matches;
228228
mod mem_replace;
229-
mod methods;
230229
mod min_ident_chars;
231230
mod minmax;
232231
mod misc;
@@ -435,8 +434,13 @@ pub fn explain(name: &str) -> i32 {
435434
///
436435
/// Used in `./src/driver.rs`.
437436
#[expect(clippy::too_many_lines)]
438-
pub fn register_lints(store: &mut rustc_lint::LintStore, conf: &'static Conf, groups: &mut RegistrationGroups) {
437+
pub fn register_lints(
438+
store: &mut rustc_lint::LintStore,
439+
conf: &'static Conf,
440+
groups: &mut RegistrationGroups,
441+
) -> FormatArgsStorage {
439442
groups.insert_lints(declared_lints::LINTS);
443+
store.register_lints(&declared_lints::LINTS.iter().map(|x| *x.lint).collect::<Vec<_>>());
440444

441445
for (old_name, new_name) in deprecated_lints::RENAMED {
442446
store.register_renamed(old_name, new_name);
@@ -502,8 +506,6 @@ pub fn register_lints(store: &mut rustc_lint::LintStore, conf: &'static Conf, gr
502506
store.register_late_pass(|_| Box::new(non_octal_unix_permissions::NonOctalUnixPermissions));
503507
store.register_early_pass(|| Box::new(unnecessary_self_imports::UnnecessarySelfImports));
504508
store.register_late_pass(move |_| Box::new(approx_const::ApproxConstant::new(conf)));
505-
let format_args = format_args_storage.clone();
506-
store.register_late_pass(move |_| Box::new(methods::Methods::new(conf, format_args.clone())));
507509
store.register_late_pass(move |_| Box::new(matches::Matches::new(conf)));
508510
store.register_late_pass(move |_| Box::new(manual_non_exhaustive::ManualNonExhaustive::new(conf)));
509511
store.register_late_pass(move |_| Box::new(manual_strip::ManualStrip::new(conf)));
@@ -827,4 +829,6 @@ pub fn register_lints(store: &mut rustc_lint::LintStore, conf: &'static Conf, gr
827829
store.register_late_pass(move |_| Box::new(redundant_test_prefix::RedundantTestPrefix));
828830
store.register_late_pass(|_| Box::new(cloned_ref_to_slice_refs::ClonedRefToSliceRefs::new(conf)));
829831
// add lints here, do not remove this comment, it's used in `new_lint`
832+
833+
format_args_storage
830834
}

0 commit comments

Comments
 (0)