Skip to content

Commit 4be144a

Browse files
committed
Auto merge of #4697 - Licenser:no-exit, r=flip1995
restriction lint for `std::process::exit` Addition to #4655 - adds the lint checking for `std::process::exit` changelog: add restriction lint for `std::process::exit`
2 parents 03806ed + 2f1370d commit 4be144a

File tree

11 files changed

+118
-2
lines changed

11 files changed

+118
-2
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/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

+4
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,7 @@ pub mod escape;
188188
pub mod eta_reduction;
189189
pub mod eval_order_dependence;
190190
pub mod excessive_precision;
191+
pub mod exit;
191192
pub mod explicit_write;
192193
pub mod fallible_impl_from;
193194
pub mod format;
@@ -501,6 +502,7 @@ pub fn register_plugins(store: &mut lint::LintStore, sess: &Session, conf: &Conf
501502
&eval_order_dependence::DIVERGING_SUB_EXPRESSION,
502503
&eval_order_dependence::EVAL_ORDER_DEPENDENCE,
503504
&excessive_precision::EXCESSIVE_PRECISION,
505+
&exit::EXIT,
504506
&explicit_write::EXPLICIT_WRITE,
505507
&fallible_impl_from::FALLIBLE_IMPL_FROM,
506508
&format::USELESS_FORMAT,
@@ -941,12 +943,14 @@ pub fn register_plugins(store: &mut lint::LintStore, sess: &Session, conf: &Conf
941943
store.register_early_pass(move || box enum_variants::EnumVariantNames::new(enum_variant_name_threshold));
942944
store.register_late_pass(|| box unused_self::UnusedSelf);
943945
store.register_late_pass(|| box mutable_debug_assertion::DebugAssertWithMutCall);
946+
store.register_late_pass(|| box exit::Exit);
944947

945948
store.register_group(true, "clippy::restriction", Some("clippy_restriction"), vec![
946949
LintId::of(&arithmetic::FLOAT_ARITHMETIC),
947950
LintId::of(&arithmetic::INTEGER_ARITHMETIC),
948951
LintId::of(&dbg_macro::DBG_MACRO),
949952
LintId::of(&else_if_without_else::ELSE_IF_WITHOUT_ELSE),
953+
LintId::of(&exit::EXIT),
950954
LintId::of(&implicit_return::IMPLICIT_RETURN),
951955
LintId::of(&indexing_slicing::INDEXING_SLICING),
952956
LintId::of(&inherent_impl::MULTIPLE_INHERENT_IMPL),

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"];

src/lintlist/mod.rs

+8-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ pub use lint::Lint;
66
pub use lint::LINT_LEVELS;
77

88
// begin lint list, do not remove this comment, it’s used in `update_lints`
9-
pub const ALL_LINTS: [Lint; 331] = [
9+
pub const ALL_LINTS: [Lint; 332] = [
1010
Lint {
1111
name: "absurd_extreme_comparisons",
1212
group: "correctness",
@@ -490,6 +490,13 @@ pub const ALL_LINTS: [Lint; 331] = [
490490
deprecation: None,
491491
module: "excessive_precision",
492492
},
493+
Lint {
494+
name: "exit",
495+
group: "restriction",
496+
desc: "`std::process::exit` is called, terminating the program",
497+
deprecation: None,
498+
module: "exit",
499+
},
493500
Lint {
494501
name: "expect_fun_call",
495502
group: "perf",

tests/ui/exit1.rs

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#[warn(clippy::exit)]
2+
3+
fn not_main() {
4+
if true {
5+
std::process::exit(4);
6+
}
7+
}
8+
9+
fn main() {
10+
if true {
11+
std::process::exit(2);
12+
};
13+
not_main();
14+
std::process::exit(1);
15+
}

tests/ui/exit1.stderr

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
error: usage of `process::exit`
2+
--> $DIR/exit1.rs:5:9
3+
|
4+
LL | std::process::exit(4);
5+
| ^^^^^^^^^^^^^^^^^^^^^
6+
|
7+
= note: `-D clippy::exit` implied by `-D warnings`
8+
9+
error: aborting due to previous error
10+

tests/ui/exit2.rs

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#[warn(clippy::exit)]
2+
3+
fn also_not_main() {
4+
std::process::exit(3);
5+
}
6+
7+
fn main() {
8+
if true {
9+
std::process::exit(2);
10+
};
11+
also_not_main();
12+
std::process::exit(1);
13+
}

tests/ui/exit2.stderr

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
error: usage of `process::exit`
2+
--> $DIR/exit2.rs:4:5
3+
|
4+
LL | std::process::exit(3);
5+
| ^^^^^^^^^^^^^^^^^^^^^
6+
|
7+
= note: `-D clippy::exit` implied by `-D warnings`
8+
9+
error: aborting due to previous error
10+

tests/ui/exit3.rs

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#[warn(clippy::exit)]
2+
3+
fn main() {
4+
if true {
5+
std::process::exit(2);
6+
};
7+
std::process::exit(1);
8+
}

0 commit comments

Comments
 (0)