Skip to content

Commit f112422

Browse files
committed
Auto merge of #4790 - flip1995:rollup-8rnsng6, r=flip1995
Rollup of 4 pull requests Successful merges: - #4697 (restriction lint for `std::process::exit`) - #4757 (Fix Deprecated lints don't expand) - #4758 (`DecimalLiteralRepresentation` simplification) - #4769 (don't warn on CRLF in `with_newline` lints) Failed merges: r? @ghost
2 parents 7531a08 + 2df272b commit f112422

18 files changed

+265
-101
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -996,6 +996,7 @@ Released 2018-09-13
996996
[`erasing_op`]: https://rust-lang.github.io/rust-clippy/master/index.html#erasing_op
997997
[`eval_order_dependence`]: https://rust-lang.github.io/rust-clippy/master/index.html#eval_order_dependence
998998
[`excessive_precision`]: https://rust-lang.github.io/rust-clippy/master/index.html#excessive_precision
999+
[`exit`]: https://rust-lang.github.io/rust-clippy/master/index.html#exit
9991000
[`expect_fun_call`]: https://rust-lang.github.io/rust-clippy/master/index.html#expect_fun_call
10001001
[`expl_impl_clone_on_copy`]: https://rust-lang.github.io/rust-clippy/master/index.html#expl_impl_clone_on_copy
10011002
[`explicit_counter_loop`]: https://rust-lang.github.io/rust-clippy/master/index.html#explicit_counter_loop

README.md

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

77
A collection of lints to catch common mistakes and improve your [Rust](https://github.com/rust-lang/rust) code.
88

9-
[There are 331 lints included in this crate!](https://rust-lang.github.io/rust-clippy/master/index.html)
9+
[There are 332 lints included in this crate!](https://rust-lang.github.io/rust-clippy/master/index.html)
1010

1111
We have a bunch of lint categories to allow you to choose how much Clippy is supposed to ~~annoy~~ help you:
1212

clippy_lints/src/deprecated_lints.rs

+61-62
Original file line numberDiff line numberDiff line change
@@ -4,129 +4,128 @@ macro_rules! declare_deprecated_lint {
44
}
55
}
66

7-
/// **What it does:** Nothing. This lint has been deprecated.
8-
///
9-
/// **Deprecation reason:** This used to check for `assert!(a == b)` and recommend
10-
/// replacement with `assert_eq!(a, b)`, but this is no longer needed after RFC 2011.
117
declare_deprecated_lint! {
8+
/// **What it does:** Nothing. This lint has been deprecated.
9+
///
10+
/// **Deprecation reason:** This used to check for `assert!(a == b)` and recommend
11+
/// replacement with `assert_eq!(a, b)`, but this is no longer needed after RFC 2011.
1212
pub SHOULD_ASSERT_EQ,
1313
"`assert!()` will be more flexible with RFC 2011"
1414
}
1515

16-
/// **What it does:** Nothing. This lint has been deprecated.
17-
///
18-
/// **Deprecation reason:** This used to check for `Vec::extend`, which was slower than
19-
/// `Vec::extend_from_slice`. Thanks to specialization, this is no longer true.
2016
declare_deprecated_lint! {
17+
/// **What it does:** Nothing. This lint has been deprecated.
18+
///
19+
/// **Deprecation reason:** This used to check for `Vec::extend`, which was slower than
20+
/// `Vec::extend_from_slice`. Thanks to specialization, this is no longer true.
2121
pub EXTEND_FROM_SLICE,
2222
"`.extend_from_slice(_)` is a faster way to extend a Vec by a slice"
2323
}
2424

25-
/// **What it does:** Nothing. This lint has been deprecated.
26-
///
27-
/// **Deprecation reason:** `Range::step_by(0)` used to be linted since it's
28-
/// an infinite iterator, which is better expressed by `iter::repeat`,
29-
/// but the method has been removed for `Iterator::step_by` which panics
30-
/// if given a zero
3125
declare_deprecated_lint! {
26+
/// **What it does:** Nothing. This lint has been deprecated.
27+
///
28+
/// **Deprecation reason:** `Range::step_by(0)` used to be linted since it's
29+
/// an infinite iterator, which is better expressed by `iter::repeat`,
30+
/// but the method has been removed for `Iterator::step_by` which panics
31+
/// if given a zero
3232
pub RANGE_STEP_BY_ZERO,
3333
"`iterator.step_by(0)` panics nowadays"
3434
}
3535

36-
/// **What it does:** Nothing. This lint has been deprecated.
37-
///
38-
/// **Deprecation reason:** This used to check for `Vec::as_slice`, which was unstable with good
39-
/// stable alternatives. `Vec::as_slice` has now been stabilized.
4036
declare_deprecated_lint! {
37+
/// **What it does:** Nothing. This lint has been deprecated.
38+
///
39+
/// **Deprecation reason:** This used to check for `Vec::as_slice`, which was unstable with good
40+
/// stable alternatives. `Vec::as_slice` has now been stabilized.
4141
pub UNSTABLE_AS_SLICE,
4242
"`Vec::as_slice` has been stabilized in 1.7"
4343
}
4444

45-
46-
/// **What it does:** Nothing. This lint has been deprecated.
47-
///
48-
/// **Deprecation reason:** This used to check for `Vec::as_mut_slice`, which was unstable with good
49-
/// stable alternatives. `Vec::as_mut_slice` has now been stabilized.
5045
declare_deprecated_lint! {
46+
/// **What it does:** Nothing. This lint has been deprecated.
47+
///
48+
/// **Deprecation reason:** This used to check for `Vec::as_mut_slice`, which was unstable with good
49+
/// stable alternatives. `Vec::as_mut_slice` has now been stabilized.
5150
pub UNSTABLE_AS_MUT_SLICE,
5251
"`Vec::as_mut_slice` has been stabilized in 1.7"
5352
}
5453

55-
/// **What it does:** Nothing. This lint has been deprecated.
56-
///
57-
/// **Deprecation reason:** This used to check for `.to_string()` method calls on values
58-
/// of type `&str`. This is not unidiomatic and with specialization coming, `to_string` could be
59-
/// specialized to be as efficient as `to_owned`.
6054
declare_deprecated_lint! {
55+
/// **What it does:** Nothing. This lint has been deprecated.
56+
///
57+
/// **Deprecation reason:** This used to check for `.to_string()` method calls on values
58+
/// of type `&str`. This is not unidiomatic and with specialization coming, `to_string` could be
59+
/// specialized to be as efficient as `to_owned`.
6160
pub STR_TO_STRING,
6261
"using `str::to_string` is common even today and specialization will likely happen soon"
6362
}
6463

65-
/// **What it does:** Nothing. This lint has been deprecated.
66-
///
67-
/// **Deprecation reason:** This used to check for `.to_string()` method calls on values
68-
/// of type `String`. This is not unidiomatic and with specialization coming, `to_string` could be
69-
/// specialized to be as efficient as `clone`.
7064
declare_deprecated_lint! {
65+
/// **What it does:** Nothing. This lint has been deprecated.
66+
///
67+
/// **Deprecation reason:** This used to check for `.to_string()` method calls on values
68+
/// of type `String`. This is not unidiomatic and with specialization coming, `to_string` could be
69+
/// specialized to be as efficient as `clone`.
7170
pub STRING_TO_STRING,
7271
"using `string::to_string` is common even today and specialization will likely happen soon"
7372
}
7473

75-
/// **What it does:** Nothing. This lint has been deprecated.
76-
///
77-
/// **Deprecation reason:** This lint should never have applied to non-pointer types, as transmuting
78-
/// between non-pointer types of differing alignment is well-defined behavior (it's semantically
79-
/// equivalent to a memcpy). This lint has thus been refactored into two separate lints:
80-
/// cast_ptr_alignment and transmute_ptr_to_ptr.
8174
declare_deprecated_lint! {
75+
/// **What it does:** Nothing. This lint has been deprecated.
76+
///
77+
/// **Deprecation reason:** This lint should never have applied to non-pointer types, as transmuting
78+
/// between non-pointer types of differing alignment is well-defined behavior (it's semantically
79+
/// equivalent to a memcpy). This lint has thus been refactored into two separate lints:
80+
/// cast_ptr_alignment and transmute_ptr_to_ptr.
8281
pub MISALIGNED_TRANSMUTE,
8382
"this lint has been split into cast_ptr_alignment and transmute_ptr_to_ptr"
8483
}
8584

86-
/// **What it does:** Nothing. This lint has been deprecated.
87-
///
88-
/// **Deprecation reason:** This lint is too subjective, not having a good reason for being in clippy.
89-
/// Additionally, compound assignment operators may be overloaded separately from their non-assigning
90-
/// counterparts, so this lint may suggest a change in behavior or the code may not compile.
9185
declare_deprecated_lint! {
86+
/// **What it does:** Nothing. This lint has been deprecated.
87+
///
88+
/// **Deprecation reason:** This lint is too subjective, not having a good reason for being in clippy.
89+
/// Additionally, compound assignment operators may be overloaded separately from their non-assigning
90+
/// counterparts, so this lint may suggest a change in behavior or the code may not compile.
9291
pub ASSIGN_OPS,
9392
"using compound assignment operators (e.g., `+=`) is harmless"
9493
}
9594

96-
/// **What it does:** Nothing. This lint has been deprecated.
97-
///
98-
/// **Deprecation reason:** The original rule will only lint for `if let`. After
99-
/// making it support to lint `match`, naming as `if let` is not suitable for it.
100-
/// So, this lint is deprecated.
10195
declare_deprecated_lint! {
96+
/// **What it does:** Nothing. This lint has been deprecated.
97+
///
98+
/// **Deprecation reason:** The original rule will only lint for `if let`. After
99+
/// making it support to lint `match`, naming as `if let` is not suitable for it.
100+
/// So, this lint is deprecated.
102101
pub IF_LET_REDUNDANT_PATTERN_MATCHING,
103102
"this lint has been changed to redundant_pattern_matching"
104103
}
105104

106-
/// **What it does:** Nothing. This lint has been deprecated.
107-
///
108-
/// **Deprecation reason:** This lint used to suggest replacing `let mut vec =
109-
/// Vec::with_capacity(n); vec.set_len(n);` with `let vec = vec![0; n];`. The
110-
/// replacement has very different performance characteristics so the lint is
111-
/// deprecated.
112105
declare_deprecated_lint! {
106+
/// **What it does:** Nothing. This lint has been deprecated.
107+
///
108+
/// **Deprecation reason:** This lint used to suggest replacing `let mut vec =
109+
/// Vec::with_capacity(n); vec.set_len(n);` with `let vec = vec![0; n];`. The
110+
/// replacement has very different performance characteristics so the lint is
111+
/// deprecated.
113112
pub UNSAFE_VECTOR_INITIALIZATION,
114113
"the replacement suggested by this lint had substantially different behavior"
115114
}
116115

117-
/// **What it does:** Nothing. This lint has been deprecated.
118-
///
119-
/// **Deprecation reason:** This lint has been superseded by the warn-by-default
120-
/// `invalid_value` rustc lint.
121116
declare_deprecated_lint! {
117+
/// **What it does:** Nothing. This lint has been deprecated.
118+
///
119+
/// **Deprecation reason:** This lint has been superseded by the warn-by-default
120+
/// `invalid_value` rustc lint.
122121
pub INVALID_REF,
123122
"superseded by rustc lint `invalid_value`"
124123
}
125124

126-
/// **What it does:** Nothing. This lint has been deprecated.
127-
///
128-
/// **Deprecation reason:** This lint has been superseded by #[must_use] in rustc.
129125
declare_deprecated_lint! {
126+
/// **What it does:** Nothing. This lint has been deprecated.
127+
///
128+
/// **Deprecation reason:** This lint has been superseded by #[must_use] in rustc.
130129
pub UNUSED_COLLECT,
131130
"`collect` has been marked as #[must_use] in rustc and that covers all cases of this lint"
132131
}

clippy_lints/src/exit.rs

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
use crate::utils::{is_entrypoint_fn, match_def_path, paths, qpath_res, span_lint};
2+
use if_chain::if_chain;
3+
use rustc::hir::{Expr, ExprKind, Item, ItemKind, Node};
4+
use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
5+
use rustc::{declare_lint_pass, declare_tool_lint};
6+
7+
declare_clippy_lint! {
8+
/// **What it does:** `exit()` terminates the program and doesn't provide a
9+
/// stack trace.
10+
///
11+
/// **Why is this bad?** Ideally a program is terminated by finishing
12+
/// the main function.
13+
///
14+
/// **Known problems:** None.
15+
///
16+
/// **Example:**
17+
/// ```ignore
18+
/// std::process::exit(0)
19+
/// ```
20+
pub EXIT,
21+
restriction,
22+
"`std::process::exit` is called, terminating the program"
23+
}
24+
25+
declare_lint_pass!(Exit => [EXIT]);
26+
27+
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Exit {
28+
fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, e: &'tcx Expr) {
29+
if_chain! {
30+
if let ExprKind::Call(ref path_expr, ref _args) = e.kind;
31+
if let ExprKind::Path(ref path) = path_expr.kind;
32+
if let Some(def_id) = qpath_res(cx, path, path_expr.hir_id).opt_def_id();
33+
if match_def_path(cx, def_id, &paths::EXIT);
34+
then {
35+
let mut parent = cx.tcx.hir().get_parent_item(e.hir_id);
36+
if let Some(Node::Item(Item{ident, kind: ItemKind::Fn(..), ..})) = cx.tcx.hir().find(parent) {
37+
// If the next item up is a function we check if it is an entry point
38+
// and only then emit a linter warning
39+
let def_id = cx.tcx.hir().local_def_id(parent);
40+
if !is_entrypoint_fn(cx, def_id) {
41+
span_lint(cx, EXIT, e.span, "usage of `process::exit`");
42+
}
43+
}
44+
}
45+
}
46+
}
47+
}

clippy_lints/src/lib.rs

+6
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ extern crate rustc_errors;
2929
#[allow(unused_extern_crates)]
3030
extern crate rustc_index;
3131
#[allow(unused_extern_crates)]
32+
extern crate rustc_lexer;
33+
#[allow(unused_extern_crates)]
3234
extern crate rustc_mir;
3335
#[allow(unused_extern_crates)]
3436
extern crate rustc_target;
@@ -188,6 +190,7 @@ pub mod escape;
188190
pub mod eta_reduction;
189191
pub mod eval_order_dependence;
190192
pub mod excessive_precision;
193+
pub mod exit;
191194
pub mod explicit_write;
192195
pub mod fallible_impl_from;
193196
pub mod format;
@@ -501,6 +504,7 @@ pub fn register_plugins(store: &mut lint::LintStore, sess: &Session, conf: &Conf
501504
&eval_order_dependence::DIVERGING_SUB_EXPRESSION,
502505
&eval_order_dependence::EVAL_ORDER_DEPENDENCE,
503506
&excessive_precision::EXCESSIVE_PRECISION,
507+
&exit::EXIT,
504508
&explicit_write::EXPLICIT_WRITE,
505509
&fallible_impl_from::FALLIBLE_IMPL_FROM,
506510
&format::USELESS_FORMAT,
@@ -941,12 +945,14 @@ pub fn register_plugins(store: &mut lint::LintStore, sess: &Session, conf: &Conf
941945
store.register_early_pass(move || box enum_variants::EnumVariantNames::new(enum_variant_name_threshold));
942946
store.register_late_pass(|| box unused_self::UnusedSelf);
943947
store.register_late_pass(|| box mutable_debug_assertion::DebugAssertWithMutCall);
948+
store.register_late_pass(|| box exit::Exit);
944949

945950
store.register_group(true, "clippy::restriction", Some("clippy_restriction"), vec![
946951
LintId::of(&arithmetic::FLOAT_ARITHMETIC),
947952
LintId::of(&arithmetic::INTEGER_ARITHMETIC),
948953
LintId::of(&dbg_macro::DBG_MACRO),
949954
LintId::of(&else_if_without_else::ELSE_IF_WITHOUT_ELSE),
955+
LintId::of(&exit::EXIT),
950956
LintId::of(&implicit_return::IMPLICIT_RETURN),
951957
LintId::of(&indexing_slicing::INDEXING_SLICING),
952958
LintId::of(&inherent_impl::MULTIPLE_INHERENT_IMPL),

clippy_lints/src/literal_representation.rs

+1-6
Original file line numberDiff line numberDiff line change
@@ -509,17 +509,12 @@ impl DecimalLiteralRepresentation {
509509
fn check_lit(self, cx: &EarlyContext<'_>, lit: &Lit) {
510510
// Lint integral literals.
511511
if_chain! {
512-
if let LitKind::Int(..) = lit.kind;
512+
if let LitKind::Int(val, _) = lit.kind;
513513
if let Some(src) = snippet_opt(cx, lit.span);
514514
if let Some(firstch) = src.chars().next();
515515
if char::to_digit(firstch, 10).is_some();
516516
let digit_info = DigitInfo::new(&src, false);
517517
if digit_info.radix == Radix::Decimal;
518-
if let Ok(val) = digit_info.digits
519-
.chars()
520-
.filter(|&c| c != '_')
521-
.collect::<String>()
522-
.parse::<u128>();
523518
if val >= u128::from(self.threshold);
524519
then {
525520
let hex = format!("{:#X}", val);

clippy_lints/src/utils/paths.rs

+1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ pub const DROP: [&str; 3] = ["core", "mem", "drop"];
2727
pub const DROP_TRAIT: [&str; 4] = ["core", "ops", "drop", "Drop"];
2828
pub const DURATION: [&str; 3] = ["core", "time", "Duration"];
2929
pub const EARLY_CONTEXT: [&str; 4] = ["rustc", "lint", "context", "EarlyContext"];
30+
pub const EXIT: [&str; 3] = ["std", "process", "exit"];
3031
pub const FMT_ARGUMENTS_NEW_V1: [&str; 4] = ["core", "fmt", "Arguments", "new_v1"];
3132
pub const FMT_ARGUMENTS_NEW_V1_FORMATTED: [&str; 4] = ["core", "fmt", "Arguments", "new_v1_formatted"];
3233
pub const FMT_ARGUMENTV1_NEW: [&str; 4] = ["core", "fmt", "ArgumentV1", "new"];

0 commit comments

Comments
 (0)