Skip to content

Commit 77794e9

Browse files
author
Michael A. Plikk
committed
Create lint for unimplemented!()
1 parent e7a3e03 commit 77794e9

File tree

6 files changed

+63
-16
lines changed

6 files changed

+63
-16
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -815,6 +815,7 @@ All notable changes to this project will be documented in this file.
815815
[`trivial_regex`]: https://rust-lang-nursery.github.io/rust-clippy/master/index.html#trivial_regex
816816
[`type_complexity`]: https://rust-lang-nursery.github.io/rust-clippy/master/index.html#type_complexity
817817
[`unicode_not_nfc`]: https://rust-lang-nursery.github.io/rust-clippy/master/index.html#unicode_not_nfc
818+
[`unimplemented`]: https://rust-lang-nursery.github.io/rust-clippy/master/index.html#unimplemented
818819
[`unit_arg`]: https://rust-lang-nursery.github.io/rust-clippy/master/index.html#unit_arg
819820
[`unit_cmp`]: https://rust-lang-nursery.github.io/rust-clippy/master/index.html#unit_cmp
820821
[`unnecessary_cast`]: https://rust-lang-nursery.github.io/rust-clippy/master/index.html#unnecessary_cast

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

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

10-
[There are 258 lints included in this crate!](https://rust-lang-nursery.github.io/rust-clippy/master/index.html)
10+
[There are 259 lints included in this crate!](https://rust-lang-nursery.github.io/rust-clippy/master/index.html)
1111

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

clippy_lints/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -627,6 +627,7 @@ pub fn register_plugins(reg: &mut rustc_plugin::Registry) {
627627
open_options::NONSENSICAL_OPEN_OPTIONS,
628628
overflow_check_conditional::OVERFLOW_CHECK_CONDITIONAL,
629629
panic::PANIC_PARAMS,
630+
panic::UNIMPLEMENTED,
630631
partialeq_ne_impl::PARTIALEQ_NE_IMPL,
631632
precedence::PRECEDENCE,
632633
ptr::CMP_NULL,
@@ -749,6 +750,7 @@ pub fn register_plugins(reg: &mut rustc_plugin::Registry) {
749750
non_expressive_names::MANY_SINGLE_CHAR_NAMES,
750751
ok_if_let::IF_LET_SOME_RESULT,
751752
panic::PANIC_PARAMS,
753+
panic::UNIMPLEMENTED,
752754
ptr::CMP_NULL,
753755
ptr::PTR_ARG,
754756
question_mark::QUESTION_MARK,

clippy_lints/src/panic.rs

Lines changed: 43 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
use rustc::hir::*;
22
use rustc::lint::*;
33
use syntax::ast::LitKind;
4-
use utils::{is_direct_expn_of, match_def_path, opt_def_id, paths, resolve_node, span_lint};
4+
use syntax::ptr::P;
5+
use utils::{is_direct_expn_of, is_expn_of, match_def_path, opt_def_id, paths, resolve_node, span_lint};
56

67
/// **What it does:** Checks for missing parameters in `panic!`.
78
///
@@ -22,12 +23,28 @@ declare_clippy_lint! {
2223
"missing parameters in `panic!` calls"
2324
}
2425

26+
/// **What it does:** Checks for usage of `unimplemented!`.
27+
///
28+
/// **Why is this bad?** This macro should not be present in production code
29+
///
30+
/// **Known problems:** None.
31+
///
32+
/// **Example:**
33+
/// ```rust
34+
/// unimplemented!();
35+
/// ```
36+
declare_clippy_lint! {
37+
pub UNIMPLEMENTED,
38+
style,
39+
"`unimplemented!` should not be present in production code"
40+
}
41+
2542
#[allow(missing_copy_implementations)]
2643
pub struct Pass;
2744

2845
impl LintPass for Pass {
2946
fn get_lints(&self) -> LintArray {
30-
lint_array!(PANIC_PARAMS)
47+
lint_array!(PANIC_PARAMS, UNIMPLEMENTED)
3148
}
3249
}
3350

@@ -37,22 +54,35 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass {
3754
if let ExprBlock(ref block, _) = expr.node;
3855
if let Some(ref ex) = block.expr;
3956
if let ExprCall(ref fun, ref params) = ex.node;
40-
if params.len() == 2;
4157
if let ExprPath(ref qpath) = fun.node;
4258
if let Some(fun_def_id) = opt_def_id(resolve_node(cx, qpath, fun.hir_id));
4359
if match_def_path(cx.tcx, fun_def_id, &paths::BEGIN_PANIC);
44-
if let ExprLit(ref lit) = params[0].node;
45-
if is_direct_expn_of(expr.span, "panic").is_some();
46-
if let LitKind::Str(ref string, _) = lit.node;
47-
let string = string.as_str().replace("{{", "").replace("}}", "");
48-
if let Some(par) = string.find('{');
49-
if string[par..].contains('}');
50-
if params[0].span.source_callee().is_none();
51-
if params[0].span.lo() != params[0].span.hi();
60+
if params.len() == 2;
5261
then {
53-
span_lint(cx, PANIC_PARAMS, params[0].span,
54-
"you probably are missing some parameter in your format string");
62+
if is_expn_of(expr.span, "unimplemented").is_some() {
63+
span_lint(cx, UNIMPLEMENTED, expr.span,
64+
"`unimplemented` should not be present in production code");
65+
} else {
66+
match_panic(params, expr, cx);
67+
}
5568
}
5669
}
5770
}
5871
}
72+
73+
fn match_panic(params: &P<[Expr]>, expr: &Expr, cx: &LateContext) {
74+
if_chain! {
75+
if let ExprLit(ref lit) = params[0].node;
76+
if is_direct_expn_of(expr.span, "panic").is_some();
77+
if let LitKind::Str(ref string, _) = lit.node;
78+
let string = string.as_str().replace("{{", "").replace("}}", "");
79+
if let Some(par) = string.find('{');
80+
if string[par..].contains('}');
81+
if params[0].span.source_callee().is_none();
82+
if params[0].span.lo() != params[0].span.hi();
83+
then {
84+
span_lint(cx, PANIC_PARAMS, params[0].span,
85+
"you probably are missing some parameter in your format string");
86+
}
87+
}
88+
}

tests/ui/panic.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

22

33

4-
#![warn(panic_params)]
4+
#![warn(panic_params, unimplemented)]
55

66
fn missing() {
77
if true {
@@ -53,6 +53,10 @@ fn ok_escaped() {
5353
panic!("{case }}");
5454
}
5555

56+
fn unimplemented() {
57+
unimplemented!();
58+
}
59+
5660
fn main() {
5761
missing();
5862
ok_single();
@@ -61,4 +65,5 @@ fn main() {
6165
ok_inner();
6266
ok_nomsg();
6367
ok_escaped();
68+
unimplemented();
6469
}

tests/ui/panic.stderr

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,14 @@ error: you probably are missing some parameter in your format string
2424
15 | panic!("{{{this}}}");
2525
| ^^^^^^^^^^^^
2626

27-
error: aborting due to 4 previous errors
27+
error: `unimplemented` should not be present in production code
28+
--> $DIR/panic.rs:57:5
29+
|
30+
57 | unimplemented!();
31+
| ^^^^^^^^^^^^^^^^^
32+
|
33+
= note: `-D unimplemented` implied by `-D warnings`
34+
= note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info)
35+
36+
error: aborting due to 5 previous errors
2837

0 commit comments

Comments
 (0)