Skip to content

Commit 1fa3d66

Browse files
committed
Merge commit 'd0cf3481a84e3aa68c2f185c460e282af36ebc42' into clippyup
1 parent 1f069c0 commit 1fa3d66

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

63 files changed

+1957
-546
lines changed

.github/ISSUE_TEMPLATE/bug_report.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ body:
1818
id: reproducer
1919
attributes:
2020
label: Reproducer
21-
description: Please provide the code and steps to repoduce the bug
21+
description: Please provide the code and steps to reproduce the bug
2222
value: |
2323
I tried this code:
2424

CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -3069,6 +3069,7 @@ Released 2018-09-13
30693069
[`bytes_nth`]: https://rust-lang.github.io/rust-clippy/master/index.html#bytes_nth
30703070
[`cargo_common_metadata`]: https://rust-lang.github.io/rust-clippy/master/index.html#cargo_common_metadata
30713071
[`case_sensitive_file_extension_comparisons`]: https://rust-lang.github.io/rust-clippy/master/index.html#case_sensitive_file_extension_comparisons
3072+
[`cast_enum_constructor`]: https://rust-lang.github.io/rust-clippy/master/index.html#cast_enum_constructor
30723073
[`cast_enum_truncation`]: https://rust-lang.github.io/rust-clippy/master/index.html#cast_enum_truncation
30733074
[`cast_lossless`]: https://rust-lang.github.io/rust-clippy/master/index.html#cast_lossless
30743075
[`cast_possible_truncation`]: https://rust-lang.github.io/rust-clippy/master/index.html#cast_possible_truncation
@@ -3366,6 +3367,7 @@ Released 2018-09-13
33663367
[`option_map_unit_fn`]: https://rust-lang.github.io/rust-clippy/master/index.html#option_map_unit_fn
33673368
[`option_option`]: https://rust-lang.github.io/rust-clippy/master/index.html#option_option
33683369
[`or_fun_call`]: https://rust-lang.github.io/rust-clippy/master/index.html#or_fun_call
3370+
[`or_then_unwrap`]: https://rust-lang.github.io/rust-clippy/master/index.html#or_then_unwrap
33693371
[`out_of_bounds_indexing`]: https://rust-lang.github.io/rust-clippy/master/index.html#out_of_bounds_indexing
33703372
[`overflow_check_conditional`]: https://rust-lang.github.io/rust-clippy/master/index.html#overflow_check_conditional
33713373
[`panic`]: https://rust-lang.github.io/rust-clippy/master/index.html#panic
@@ -3506,6 +3508,7 @@ Released 2018-09-13
35063508
[`unnecessary_filter_map`]: https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_filter_map
35073509
[`unnecessary_find_map`]: https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_find_map
35083510
[`unnecessary_fold`]: https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_fold
3511+
[`unnecessary_join`]: https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_join
35093512
[`unnecessary_lazy_evaluations`]: https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_lazy_evaluations
35103513
[`unnecessary_mut_passed`]: https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_mut_passed
35113514
[`unnecessary_operation`]: https://rust-lang.github.io/rust-clippy/master/index.html#unnecessary_operation
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
use clippy_utils::diagnostics::span_lint;
2+
use rustc_hir::def::{CtorKind, CtorOf, DefKind, Res};
3+
use rustc_hir::{Expr, ExprKind};
4+
use rustc_lint::LateContext;
5+
use rustc_middle::ty::{self, Ty};
6+
7+
use super::CAST_ENUM_CONSTRUCTOR;
8+
9+
pub(super) fn check(cx: &LateContext<'_>, expr: &Expr<'_>, cast_expr: &Expr<'_>, cast_from: Ty<'_>) {
10+
if matches!(cast_from.kind(), ty::FnDef(..))
11+
&& let ExprKind::Path(path) = &cast_expr.kind
12+
&& let Res::Def(DefKind::Ctor(CtorOf::Variant, CtorKind::Fn), _) = cx.qpath_res(path, cast_expr.hir_id)
13+
{
14+
span_lint(
15+
cx,
16+
CAST_ENUM_CONSTRUCTOR,
17+
expr.span,
18+
"cast of an enum tuple constructor to an integer",
19+
);
20+
}
21+
}

clippy_lints/src/casts/mod.rs

+21
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
mod cast_enum_constructor;
12
mod cast_lossless;
23
mod cast_possible_truncation;
34
mod cast_possible_wrap;
@@ -454,6 +455,24 @@ declare_clippy_lint! {
454455
"casting using `as` between raw pointers to slices of types with different sizes"
455456
}
456457

458+
declare_clippy_lint! {
459+
/// ### What it does
460+
/// Checks for casts from an enum tuple constructor to an integer.
461+
///
462+
/// ### Why is this bad?
463+
/// The cast is easily confused with casting a c-like enum value to an integer.
464+
///
465+
/// ### Example
466+
/// ```rust
467+
/// enum E { X(i32) };
468+
/// let _ = E::X as usize;
469+
/// ```
470+
#[clippy::version = "1.61.0"]
471+
pub CAST_ENUM_CONSTRUCTOR,
472+
suspicious,
473+
"casts from an enum tuple constructor to an integer"
474+
}
475+
457476
pub struct Casts {
458477
msrv: Option<RustcVersion>,
459478
}
@@ -481,6 +500,7 @@ impl_lint_pass!(Casts => [
481500
CHAR_LIT_AS_U8,
482501
PTR_AS_PTR,
483502
CAST_ENUM_TRUNCATION,
503+
CAST_ENUM_CONSTRUCTOR
484504
]);
485505

486506
impl<'tcx> LateLintPass<'tcx> for Casts {
@@ -518,6 +538,7 @@ impl<'tcx> LateLintPass<'tcx> for Casts {
518538
cast_sign_loss::check(cx, expr, cast_expr, cast_from, cast_to);
519539
}
520540
cast_lossless::check(cx, expr, cast_expr, cast_from, cast_to, &self.msrv);
541+
cast_enum_constructor::check(cx, expr, cast_expr, cast_from);
521542
}
522543
}
523544

clippy_lints/src/collapsible_if.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ declare_clippy_lint! {
4242
///
4343
/// Should be written:
4444
///
45-
/// ```rust.ignore
45+
/// ```rust,ignore
4646
/// if x && y {
4747
/// …
4848
/// }
@@ -76,7 +76,7 @@ declare_clippy_lint! {
7676
///
7777
/// Should be written:
7878
///
79-
/// ```rust.ignore
79+
/// ```rust,ignore
8080
/// if x {
8181
/// …
8282
/// } else if y {

clippy_lints/src/doc.rs

+7-8
Original file line numberDiff line numberDiff line change
@@ -637,12 +637,6 @@ fn check_code(cx: &LateContext<'_>, text: &str, edition: Edition, span: Span) {
637637
loop {
638638
match parser.parse_item(ForceCollect::No) {
639639
Ok(Some(item)) => match &item.kind {
640-
// Tests with one of these items are ignored
641-
ItemKind::Static(..)
642-
| ItemKind::Const(..)
643-
| ItemKind::ExternCrate(..)
644-
| ItemKind::ForeignMod(..) => return false,
645-
// We found a main function ...
646640
ItemKind::Fn(box Fn {
647641
sig, body: Some(block), ..
648642
}) if item.ident.name == sym::main => {
@@ -661,8 +655,13 @@ fn check_code(cx: &LateContext<'_>, text: &str, edition: Edition, span: Span) {
661655
return false;
662656
}
663657
},
664-
// Another function was found; this case is ignored too
665-
ItemKind::Fn(..) => return false,
658+
// Tests with one of these items are ignored
659+
ItemKind::Static(..)
660+
| ItemKind::Const(..)
661+
| ItemKind::ExternCrate(..)
662+
| ItemKind::ForeignMod(..)
663+
// Another function was found; this case is ignored
664+
| ItemKind::Fn(..) => return false,
666665
_ => {},
667666
},
668667
Ok(None) => break,

clippy_lints/src/lib.register_all.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ store.register_group(true, "clippy::all", Some("clippy_all"), vec![
2323
LintId::of(bool_assert_comparison::BOOL_ASSERT_COMPARISON),
2424
LintId::of(booleans::LOGIC_BUG),
2525
LintId::of(booleans::NONMINIMAL_BOOL),
26+
LintId::of(casts::CAST_ENUM_CONSTRUCTOR),
2627
LintId::of(casts::CAST_ENUM_TRUNCATION),
2728
LintId::of(casts::CAST_REF_TO_MUT),
2829
LintId::of(casts::CAST_SLICE_DIFFERENT_SIZES),
@@ -166,7 +167,6 @@ store.register_group(true, "clippy::all", Some("clippy_all"), vec![
166167
LintId::of(methods::ITER_NTH_ZERO),
167168
LintId::of(methods::ITER_OVEREAGER_CLONED),
168169
LintId::of(methods::ITER_SKIP_NEXT),
169-
LintId::of(methods::ITER_WITH_DRAIN),
170170
LintId::of(methods::MANUAL_FILTER_MAP),
171171
LintId::of(methods::MANUAL_FIND_MAP),
172172
LintId::of(methods::MANUAL_SATURATING_ARITHMETIC),
@@ -182,6 +182,7 @@ store.register_group(true, "clippy::all", Some("clippy_all"), vec![
182182
LintId::of(methods::OPTION_FILTER_MAP),
183183
LintId::of(methods::OPTION_MAP_OR_NONE),
184184
LintId::of(methods::OR_FUN_CALL),
185+
LintId::of(methods::OR_THEN_UNWRAP),
185186
LintId::of(methods::RESULT_MAP_OR_INTO_OPTION),
186187
LintId::of(methods::SEARCH_IS_SOME),
187188
LintId::of(methods::SHOULD_IMPLEMENT_TRAIT),
@@ -290,7 +291,6 @@ store.register_group(true, "clippy::all", Some("clippy_all"), vec![
290291
LintId::of(transmute::UNSOUND_COLLECTION_TRANSMUTE),
291292
LintId::of(transmute::WRONG_TRANSMUTE),
292293
LintId::of(transmuting_null::TRANSMUTING_NULL),
293-
LintId::of(try_err::TRY_ERR),
294294
LintId::of(types::BORROWED_BOX),
295295
LintId::of(types::BOX_COLLECTION),
296296
LintId::of(types::REDUNDANT_ALLOCATION),

clippy_lints/src/lib.register_complexity.rs

+1
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ store.register_group(true, "clippy::complexity", Some("clippy_complexity"), vec!
4747
LintId::of(methods::NEEDLESS_SPLITN),
4848
LintId::of(methods::OPTION_AS_REF_DEREF),
4949
LintId::of(methods::OPTION_FILTER_MAP),
50+
LintId::of(methods::OR_THEN_UNWRAP),
5051
LintId::of(methods::SEARCH_IS_SOME),
5152
LintId::of(methods::SKIP_WHILE_NEXT),
5253
LintId::of(methods::UNNECESSARY_FILTER_MAP),

clippy_lints/src/lib.register_lints.rs

+3
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ store.register_lints(&[
7070
cargo::REDUNDANT_FEATURE_NAMES,
7171
cargo::WILDCARD_DEPENDENCIES,
7272
case_sensitive_file_extension_comparisons::CASE_SENSITIVE_FILE_EXTENSION_COMPARISONS,
73+
casts::CAST_ENUM_CONSTRUCTOR,
7374
casts::CAST_ENUM_TRUNCATION,
7475
casts::CAST_LOSSLESS,
7576
casts::CAST_POSSIBLE_TRUNCATION,
@@ -319,6 +320,7 @@ store.register_lints(&[
319320
methods::OPTION_FILTER_MAP,
320321
methods::OPTION_MAP_OR_NONE,
321322
methods::OR_FUN_CALL,
323+
methods::OR_THEN_UNWRAP,
322324
methods::RESULT_MAP_OR_INTO_OPTION,
323325
methods::SEARCH_IS_SOME,
324326
methods::SHOULD_IMPLEMENT_TRAIT,
@@ -332,6 +334,7 @@ store.register_lints(&[
332334
methods::UNNECESSARY_FILTER_MAP,
333335
methods::UNNECESSARY_FIND_MAP,
334336
methods::UNNECESSARY_FOLD,
337+
methods::UNNECESSARY_JOIN,
335338
methods::UNNECESSARY_LAZY_EVALUATIONS,
336339
methods::UNNECESSARY_TO_OWNED,
337340
methods::UNWRAP_OR_ELSE_DEFAULT,

clippy_lints/src/lib.register_nursery.rs

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ store.register_group(true, "clippy::nursery", Some("clippy_nursery"), vec![
1313
LintId::of(future_not_send::FUTURE_NOT_SEND),
1414
LintId::of(index_refutable_slice::INDEX_REFUTABLE_SLICE),
1515
LintId::of(let_if_seq::USELESS_LET_IF_SEQ),
16+
LintId::of(methods::ITER_WITH_DRAIN),
1617
LintId::of(missing_const_for_fn::MISSING_CONST_FOR_FN),
1718
LintId::of(mutable_debug_assertion::DEBUG_ASSERT_WITH_MUT_CALL),
1819
LintId::of(mutex_atomic::MUTEX_ATOMIC),

clippy_lints/src/lib.register_pedantic.rs

+1
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ store.register_group(true, "clippy::pedantic", Some("clippy_pedantic"), vec![
6363
LintId::of(methods::IMPLICIT_CLONE),
6464
LintId::of(methods::INEFFICIENT_TO_STRING),
6565
LintId::of(methods::MAP_UNWRAP_OR),
66+
LintId::of(methods::UNNECESSARY_JOIN),
6667
LintId::of(misc::FLOAT_CMP),
6768
LintId::of(misc::USED_UNDERSCORE_BINDING),
6869
LintId::of(mut_mut::MUT_MUT),

clippy_lints/src/lib.register_perf.rs

-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ store.register_group(true, "clippy::perf", Some("clippy_perf"), vec![
1616
LintId::of(methods::EXTEND_WITH_DRAIN),
1717
LintId::of(methods::ITER_NTH),
1818
LintId::of(methods::ITER_OVEREAGER_CLONED),
19-
LintId::of(methods::ITER_WITH_DRAIN),
2019
LintId::of(methods::MANUAL_STR_REPEAT),
2120
LintId::of(methods::OR_FUN_CALL),
2221
LintId::of(methods::SINGLE_CHAR_PATTERN),

clippy_lints/src/lib.register_restriction.rs

+1
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ store.register_group(true, "clippy::restriction", Some("clippy_restriction"), ve
6262
LintId::of(strings::STRING_SLICE),
6363
LintId::of(strings::STRING_TO_STRING),
6464
LintId::of(strings::STR_TO_STRING),
65+
LintId::of(try_err::TRY_ERR),
6566
LintId::of(types::RC_BUFFER),
6667
LintId::of(types::RC_MUTEX),
6768
LintId::of(undocumented_unsafe_blocks::UNDOCUMENTED_UNSAFE_BLOCKS),

clippy_lints/src/lib.register_style.rs

-1
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,6 @@ store.register_group(true, "clippy::style", Some("clippy_style"), vec![
105105
LintId::of(single_component_path_imports::SINGLE_COMPONENT_PATH_IMPORTS),
106106
LintId::of(tabs_in_doc_comments::TABS_IN_DOC_COMMENTS),
107107
LintId::of(to_digit_is_some::TO_DIGIT_IS_SOME),
108-
LintId::of(try_err::TRY_ERR),
109108
LintId::of(unsafe_removed_from_name::UNSAFE_REMOVED_FROM_NAME),
110109
LintId::of(unused_unit::UNUSED_UNIT),
111110
LintId::of(upper_case_acronyms::UPPER_CASE_ACRONYMS),

clippy_lints/src/lib.register_suspicious.rs

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ store.register_group(true, "clippy::suspicious", Some("clippy_suspicious"), vec!
77
LintId::of(attrs::BLANKET_CLIPPY_RESTRICTION_LINTS),
88
LintId::of(await_holding_invalid::AWAIT_HOLDING_LOCK),
99
LintId::of(await_holding_invalid::AWAIT_HOLDING_REFCELL_REF),
10+
LintId::of(casts::CAST_ENUM_CONSTRUCTOR),
1011
LintId::of(casts::CAST_ENUM_TRUNCATION),
1112
LintId::of(eval_order_dependence::EVAL_ORDER_DEPENDENCE),
1213
LintId::of(float_equality_without_abs::FLOAT_EQUALITY_WITHOUT_ABS),

clippy_lints/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
// FIXME: switch to something more ergonomic here, once available.
2525
// (Currently there is no way to opt into sysroot crates without `extern crate`.)
26+
extern crate rustc_arena;
2627
extern crate rustc_ast;
2728
extern crate rustc_ast_pretty;
2829
extern crate rustc_attr;

0 commit comments

Comments
 (0)