Skip to content

Commit 8325d48

Browse files
committed
Auto merge of #6351 - flip1995:rustup, r=flip1995
Rustup r? `@ghost` changelog: none
2 parents b2aefb8 + 5ee0a40 commit 8325d48

16 files changed

+129
-195
lines changed

clippy_lints/src/assertions_on_constants.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use crate::consts::{constant, Constant};
2-
use crate::utils::paths;
3-
use crate::utils::{is_direct_expn_of, is_expn_of, match_function_call, snippet_opt, span_lint_and_help};
2+
use crate::utils::{is_direct_expn_of, is_expn_of, match_panic_call, snippet_opt, span_lint_and_help};
43
use if_chain::if_chain;
54
use rustc_ast::ast::LitKind;
65
use rustc_hir::{Expr, ExprKind, PatKind, UnOp};
@@ -130,10 +129,13 @@ fn match_assert_with_message<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>)
130129
if let ExprKind::Block(ref block, _) = arms[0].body.kind;
131130
if block.stmts.is_empty();
132131
if let Some(block_expr) = &block.expr;
133-
if let ExprKind::Block(ref inner_block, _) = block_expr.kind;
134-
if let Some(begin_panic_call) = &inner_block.expr;
132+
// inner block is optional. unwarp it if it exists, or use the expression as is otherwise.
133+
if let Some(begin_panic_call) = match block_expr.kind {
134+
ExprKind::Block(ref inner_block, _) => &inner_block.expr,
135+
_ => &block.expr,
136+
};
135137
// function call
136-
if let Some(args) = match_function_call(cx, begin_panic_call, &paths::BEGIN_PANIC);
138+
if let Some(args) = match_panic_call(cx, begin_panic_call);
137139
if args.len() == 1;
138140
// bind the second argument of the `assert!` macro if it exists
139141
if let panic_message = snippet_opt(cx, args[0].span);

clippy_lints/src/attrs.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! checks for attributes
22
33
use crate::utils::{
4-
first_line_of_span, is_present_in_source, match_def_path, paths, snippet_opt, span_lint, span_lint_and_help,
4+
first_line_of_span, is_present_in_source, match_panic_def_id, snippet_opt, span_lint, span_lint_and_help,
55
span_lint_and_sugg, span_lint_and_then, without_block_comments,
66
};
77
use if_chain::if_chain;
@@ -513,7 +513,7 @@ fn is_relevant_expr(cx: &LateContext<'_>, typeck_results: &ty::TypeckResults<'_>
513513
typeck_results
514514
.qpath_res(qpath, path_expr.hir_id)
515515
.opt_def_id()
516-
.map_or(true, |fun_id| !match_def_path(cx, fun_id, &paths::BEGIN_PANIC))
516+
.map_or(true, |fun_id| !match_panic_def_id(cx, fun_id))
517517
} else {
518518
true
519519
}

clippy_lints/src/deprecated_lints.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,3 +181,8 @@ declare_deprecated_lint! {
181181
pub TEMPORARY_CSTRING_AS_PTR,
182182
"this lint has been uplifted to rustc and is now called `temporary_cstring_as_ptr`"
183183
}
184+
185+
declare_deprecated_lint! {
186+
pub PANIC_PARAMS,
187+
"this lint has been uplifted to rustc and is now called `panic_fmt`"
188+
}

clippy_lints/src/fallible_impl_from.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
use crate::utils::paths::{BEGIN_PANIC, BEGIN_PANIC_FMT, FROM_TRAIT};
2-
use crate::utils::{is_expn_of, is_type_diagnostic_item, match_def_path, method_chain_args, span_lint_and_then};
1+
use crate::utils::paths::FROM_TRAIT;
2+
use crate::utils::{
3+
is_expn_of, is_type_diagnostic_item, match_def_path, match_panic_def_id, method_chain_args, span_lint_and_then,
4+
};
35
use if_chain::if_chain;
46
use rustc_hir as hir;
57
use rustc_lint::{LateContext, LateLintPass};
@@ -84,8 +86,7 @@ fn lint_impl_body<'tcx>(cx: &LateContext<'tcx>, impl_span: Span, impl_items: &[h
8486
if let ExprKind::Call(ref func_expr, _) = expr.kind;
8587
if let ExprKind::Path(QPath::Resolved(_, ref path)) = func_expr.kind;
8688
if let Some(path_def_id) = path.res.opt_def_id();
87-
if match_def_path(self.lcx, path_def_id, &BEGIN_PANIC) ||
88-
match_def_path(self.lcx, path_def_id, &BEGIN_PANIC_FMT);
89+
if match_panic_def_id(self.lcx, path_def_id);
8990
if is_expn_of(expr.span, "unreachable").is_none();
9091
then {
9192
self.result.push(expr.span);

clippy_lints/src/implicit_return.rs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
1-
use crate::utils::{
2-
fn_has_unsatisfiable_preds, match_def_path,
3-
paths::{BEGIN_PANIC, BEGIN_PANIC_FMT},
4-
snippet_opt, span_lint_and_then,
5-
};
1+
use crate::utils::{fn_has_unsatisfiable_preds, match_panic_def_id, snippet_opt, span_lint_and_then};
62
use if_chain::if_chain;
73
use rustc_errors::Applicability;
84
use rustc_hir::intravisit::FnKind;
@@ -109,8 +105,7 @@ fn expr_match(cx: &LateContext<'_>, expr: &Expr<'_>) {
109105
if_chain! {
110106
if let ExprKind::Path(qpath) = &expr.kind;
111107
if let Some(path_def_id) = cx.qpath_res(qpath, expr.hir_id).opt_def_id();
112-
if match_def_path(cx, path_def_id, &BEGIN_PANIC) ||
113-
match_def_path(cx, path_def_id, &BEGIN_PANIC_FMT);
108+
if match_panic_def_id(cx, path_def_id);
114109
then { }
115110
else {
116111
lint(cx, expr.span, expr.span, LINT_RETURN)

clippy_lints/src/lib.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -496,6 +496,10 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
496496
"clippy::temporary_cstring_as_ptr",
497497
"this lint has been uplifted to rustc and is now called `temporary_cstring_as_ptr`",
498498
);
499+
store.register_removed(
500+
"clippy::panic_params",
501+
"this lint has been uplifted to rustc and is now called `panic_fmt`",
502+
);
499503
// end deprecated lints, do not remove this comment, it’s used in `update_lints`
500504

501505
// begin register lints, do not remove this comment, it’s used in `update_lints`
@@ -790,7 +794,6 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
790794
&overflow_check_conditional::OVERFLOW_CHECK_CONDITIONAL,
791795
&panic_in_result_fn::PANIC_IN_RESULT_FN,
792796
&panic_unimplemented::PANIC,
793-
&panic_unimplemented::PANIC_PARAMS,
794797
&panic_unimplemented::TODO,
795798
&panic_unimplemented::UNIMPLEMENTED,
796799
&panic_unimplemented::UNREACHABLE,
@@ -1505,7 +1508,6 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
15051508
LintId::of(&open_options::NONSENSICAL_OPEN_OPTIONS),
15061509
LintId::of(&option_env_unwrap::OPTION_ENV_UNWRAP),
15071510
LintId::of(&overflow_check_conditional::OVERFLOW_CHECK_CONDITIONAL),
1508-
LintId::of(&panic_unimplemented::PANIC_PARAMS),
15091511
LintId::of(&partialeq_ne_impl::PARTIALEQ_NE_IMPL),
15101512
LintId::of(&precedence::PRECEDENCE),
15111513
LintId::of(&ptr::CMP_NULL),
@@ -1674,7 +1676,6 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
16741676
LintId::of(&non_copy_const::DECLARE_INTERIOR_MUTABLE_CONST),
16751677
LintId::of(&non_expressive_names::JUST_UNDERSCORES_AND_DIGITS),
16761678
LintId::of(&non_expressive_names::MANY_SINGLE_CHAR_NAMES),
1677-
LintId::of(&panic_unimplemented::PANIC_PARAMS),
16781679
LintId::of(&ptr::CMP_NULL),
16791680
LintId::of(&ptr::PTR_ARG),
16801681
LintId::of(&ptr_eq::PTR_EQ),

clippy_lints/src/panic_unimplemented.rs

Lines changed: 23 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,10 @@
1-
use crate::utils::{is_direct_expn_of, is_expn_of, match_function_call, paths, span_lint};
1+
use crate::utils::{is_expn_of, match_panic_call, span_lint};
22
use if_chain::if_chain;
3-
use rustc_ast::ast::LitKind;
4-
use rustc_hir::{Expr, ExprKind};
3+
use rustc_hir::Expr;
54
use rustc_lint::{LateContext, LateLintPass};
65
use rustc_session::{declare_lint_pass, declare_tool_lint};
76
use rustc_span::Span;
87

9-
declare_clippy_lint! {
10-
/// **What it does:** Checks for missing parameters in `panic!`.
11-
///
12-
/// **Why is this bad?** Contrary to the `format!` family of macros, there are
13-
/// two forms of `panic!`: if there are no parameters given, the first argument
14-
/// is not a format string and used literally. So while `format!("{}")` will
15-
/// fail to compile, `panic!("{}")` will not.
16-
///
17-
/// **Known problems:** None.
18-
///
19-
/// **Example:**
20-
/// ```no_run
21-
/// panic!("This `panic!` is probably missing a parameter there: {}");
22-
/// ```
23-
pub PANIC_PARAMS,
24-
style,
25-
"missing parameters in `panic!` calls"
26-
}
27-
288
declare_clippy_lint! {
299
/// **What it does:** Checks for usage of `panic!`.
3010
///
@@ -89,31 +69,30 @@ declare_clippy_lint! {
8969
"`unreachable!` should not be present in production code"
9070
}
9171

92-
declare_lint_pass!(PanicUnimplemented => [PANIC_PARAMS, UNIMPLEMENTED, UNREACHABLE, TODO, PANIC]);
72+
declare_lint_pass!(PanicUnimplemented => [UNIMPLEMENTED, UNREACHABLE, TODO, PANIC]);
9373

9474
impl<'tcx> LateLintPass<'tcx> for PanicUnimplemented {
9575
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
96-
if_chain! {
97-
if let ExprKind::Block(ref block, _) = expr.kind;
98-
if let Some(ref ex) = block.expr;
99-
if let Some(params) = match_function_call(cx, ex, &paths::BEGIN_PANIC)
100-
.or_else(|| match_function_call(cx, ex, &paths::BEGIN_PANIC_FMT));
101-
then {
102-
let span = get_outer_span(expr);
103-
if is_expn_of(expr.span, "unimplemented").is_some() {
104-
span_lint(cx, UNIMPLEMENTED, span,
105-
"`unimplemented` should not be present in production code");
106-
} else if is_expn_of(expr.span, "todo").is_some() {
107-
span_lint(cx, TODO, span,
108-
"`todo` should not be present in production code");
109-
} else if is_expn_of(expr.span, "unreachable").is_some() {
110-
span_lint(cx, UNREACHABLE, span,
111-
"`unreachable` should not be present in production code");
112-
} else if is_expn_of(expr.span, "panic").is_some() {
113-
span_lint(cx, PANIC, span,
114-
"`panic` should not be present in production code");
115-
match_panic(params, expr, cx);
116-
}
76+
if match_panic_call(cx, expr).is_some() {
77+
let span = get_outer_span(expr);
78+
if is_expn_of(expr.span, "unimplemented").is_some() {
79+
span_lint(
80+
cx,
81+
UNIMPLEMENTED,
82+
span,
83+
"`unimplemented` should not be present in production code",
84+
);
85+
} else if is_expn_of(expr.span, "todo").is_some() {
86+
span_lint(cx, TODO, span, "`todo` should not be present in production code");
87+
} else if is_expn_of(expr.span, "unreachable").is_some() {
88+
span_lint(
89+
cx,
90+
UNREACHABLE,
91+
span,
92+
"`unreachable` should not be present in production code",
93+
);
94+
} else if is_expn_of(expr.span, "panic").is_some() {
95+
span_lint(cx, PANIC, span, "`panic` should not be present in production code");
11796
}
11897
}
11998
}
@@ -132,20 +111,3 @@ fn get_outer_span(expr: &Expr<'_>) -> Span {
132111
}
133112
}
134113
}
135-
136-
fn match_panic(params: &[Expr<'_>], expr: &Expr<'_>, cx: &LateContext<'_>) {
137-
if_chain! {
138-
if let ExprKind::Lit(ref lit) = params[0].kind;
139-
if is_direct_expn_of(expr.span, "panic").is_some();
140-
if let LitKind::Str(ref string, _) = lit.node;
141-
let string = string.as_str().replace("{{", "").replace("}}", "");
142-
if let Some(par) = string.find('{');
143-
if string[par..].contains('}');
144-
if params[0].span.source_callee().is_none();
145-
if params[0].span.lo() != params[0].span.hi();
146-
then {
147-
span_lint(cx, PANIC_PARAMS, params[0].span,
148-
"you probably are missing some parameter in your format string");
149-
}
150-
}
151-
}

clippy_lints/src/utils/mod.rs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1233,7 +1233,7 @@ pub fn has_iter_method(cx: &LateContext<'_>, probably_ref_ty: Ty<'_>) -> Option<
12331233
/// Usage:
12341234
///
12351235
/// ```rust,ignore
1236-
/// if let Some(args) = match_function_call(cx, begin_panic_call, &paths::BEGIN_PANIC);
1236+
/// if let Some(args) = match_function_call(cx, cmp_max_call, &paths::CMP_MAX);
12371237
/// ```
12381238
pub fn match_function_call<'tcx>(
12391239
cx: &LateContext<'tcx>,
@@ -1268,6 +1268,24 @@ pub fn match_def_path<'tcx>(cx: &LateContext<'tcx>, did: DefId, syms: &[&str]) -
12681268
cx.match_def_path(did, &syms)
12691269
}
12701270

1271+
pub fn match_panic_call<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) -> Option<&'tcx [Expr<'tcx>]> {
1272+
match_function_call(cx, expr, &paths::BEGIN_PANIC)
1273+
.or_else(|| match_function_call(cx, expr, &paths::BEGIN_PANIC_FMT))
1274+
.or_else(|| match_function_call(cx, expr, &paths::PANIC_ANY))
1275+
.or_else(|| match_function_call(cx, expr, &paths::PANICKING_PANIC))
1276+
.or_else(|| match_function_call(cx, expr, &paths::PANICKING_PANIC_FMT))
1277+
.or_else(|| match_function_call(cx, expr, &paths::PANICKING_PANIC_STR))
1278+
}
1279+
1280+
pub fn match_panic_def_id(cx: &LateContext<'_>, did: DefId) -> bool {
1281+
match_def_path(cx, did, &paths::BEGIN_PANIC)
1282+
|| match_def_path(cx, did, &paths::BEGIN_PANIC_FMT)
1283+
|| match_def_path(cx, did, &paths::PANIC_ANY)
1284+
|| match_def_path(cx, did, &paths::PANICKING_PANIC)
1285+
|| match_def_path(cx, did, &paths::PANICKING_PANIC_FMT)
1286+
|| match_def_path(cx, did, &paths::PANICKING_PANIC_STR)
1287+
}
1288+
12711289
/// Returns the list of condition expressions and the list of blocks in a
12721290
/// sequence of `if/else`.
12731291
/// E.g., this returns `([a, b], [c, d, e])` for the expression

clippy_lints/src/utils/paths.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ pub const ANY_TRAIT: [&str; 3] = ["std", "any", "Any"];
88
pub const ARC_PTR_EQ: [&str; 4] = ["alloc", "sync", "Arc", "ptr_eq"];
99
pub const ASMUT_TRAIT: [&str; 3] = ["core", "convert", "AsMut"];
1010
pub const ASREF_TRAIT: [&str; 3] = ["core", "convert", "AsRef"];
11-
pub const BEGIN_PANIC: [&str; 3] = ["std", "panicking", "begin_panic"];
12-
pub const BEGIN_PANIC_FMT: [&str; 3] = ["std", "panicking", "begin_panic_fmt"];
11+
pub(super) const BEGIN_PANIC: [&str; 3] = ["std", "panicking", "begin_panic"];
12+
pub(super) const BEGIN_PANIC_FMT: [&str; 3] = ["std", "panicking", "begin_panic_fmt"];
1313
pub const BINARY_HEAP: [&str; 4] = ["alloc", "collections", "binary_heap", "BinaryHeap"];
1414
pub const BORROW_TRAIT: [&str; 3] = ["core", "borrow", "Borrow"];
1515
pub const BOX: [&str; 3] = ["alloc", "boxed", "Box"];
@@ -78,6 +78,10 @@ pub const ORD: [&str; 3] = ["core", "cmp", "Ord"];
7878
pub const OS_STRING: [&str; 4] = ["std", "ffi", "os_str", "OsString"];
7979
pub const OS_STRING_AS_OS_STR: [&str; 5] = ["std", "ffi", "os_str", "OsString", "as_os_str"];
8080
pub const OS_STR_TO_OS_STRING: [&str; 5] = ["std", "ffi", "os_str", "OsStr", "to_os_string"];
81+
pub(super) const PANICKING_PANIC: [&str; 3] = ["core", "panicking", "panic"];
82+
pub(super) const PANICKING_PANIC_FMT: [&str; 3] = ["core", "panicking", "panic_fmt"];
83+
pub(super) const PANICKING_PANIC_STR: [&str; 3] = ["core", "panicking", "panic_str"];
84+
pub(super) const PANIC_ANY: [&str; 3] = ["std", "panic", "panic_any"];
8185
pub const PARKING_LOT_MUTEX_GUARD: [&str; 2] = ["parking_lot", "MutexGuard"];
8286
pub const PARKING_LOT_RWLOCK_READ_GUARD: [&str; 2] = ["parking_lot", "RwLockReadGuard"];
8387
pub const PARKING_LOT_RWLOCK_WRITE_GUARD: [&str; 2] = ["parking_lot", "RwLockWriteGuard"];

src/lintlist/mod.rs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1838,13 +1838,6 @@ vec![
18381838
deprecation: None,
18391839
module: "panic_in_result_fn",
18401840
},
1841-
Lint {
1842-
name: "panic_params",
1843-
group: "style",
1844-
desc: "missing parameters in `panic!` calls",
1845-
deprecation: None,
1846-
module: "panic_unimplemented",
1847-
},
18481841
Lint {
18491842
name: "panicking_unwrap",
18501843
group: "correctness",

tests/ui/deprecated.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,6 @@
1010
#[warn(clippy::regex_macro)]
1111
#[warn(clippy::drop_bounds)]
1212
#[warn(clippy::temporary_cstring_as_ptr)]
13+
#[warn(clippy::panic_params)]
1314

1415
fn main() {}

tests/ui/deprecated.stderr

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,11 +72,17 @@ error: lint `clippy::temporary_cstring_as_ptr` has been removed: `this lint has
7272
LL | #[warn(clippy::temporary_cstring_as_ptr)]
7373
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
7474

75+
error: lint `clippy::panic_params` has been removed: `this lint has been uplifted to rustc and is now called `panic_fmt``
76+
--> $DIR/deprecated.rs:13:8
77+
|
78+
LL | #[warn(clippy::panic_params)]
79+
| ^^^^^^^^^^^^^^^^^^^^
80+
7581
error: lint `clippy::str_to_string` has been removed: `using `str::to_string` is common even today and specialization will likely happen soon`
7682
--> $DIR/deprecated.rs:1:8
7783
|
7884
LL | #[warn(clippy::str_to_string)]
7985
| ^^^^^^^^^^^^^^^^^^^^^
8086

81-
error: aborting due to 13 previous errors
87+
error: aborting due to 14 previous errors
8288

tests/ui/panic.rs

Lines changed: 0 additions & 61 deletions
This file was deleted.

0 commit comments

Comments
 (0)