Skip to content

Commit bd7f9c6

Browse files
committed
Merge remote-tracking branch 'upstream/master' into sync-from-rust
2 parents 0e527ba + 50bca8a commit bd7f9c6

31 files changed

+722
-109
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2024,6 +2024,7 @@ Released 2018-09-13
20242024
[`redundant_closure`]: https://rust-lang.github.io/rust-clippy/master/index.html#redundant_closure
20252025
[`redundant_closure_call`]: https://rust-lang.github.io/rust-clippy/master/index.html#redundant_closure_call
20262026
[`redundant_closure_for_method_calls`]: https://rust-lang.github.io/rust-clippy/master/index.html#redundant_closure_for_method_calls
2027+
[`redundant_else`]: https://rust-lang.github.io/rust-clippy/master/index.html#redundant_else
20272028
[`redundant_field_names`]: https://rust-lang.github.io/rust-clippy/master/index.html#redundant_field_names
20282029
[`redundant_pattern`]: https://rust-lang.github.io/rust-clippy/master/index.html#redundant_pattern
20292030
[`redundant_pattern_matching`]: https://rust-lang.github.io/rust-clippy/master/index.html#redundant_pattern_matching

clippy_lints/src/doc.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -480,7 +480,7 @@ fn check_code(cx: &LateContext<'_>, text: &str, span: Span) {
480480
| ItemKind::ForeignMod(..) => return false,
481481
// We found a main function ...
482482
ItemKind::Fn(_, sig, _, Some(block)) if item.ident.name == sym::main => {
483-
let is_async = matches!(sig.header.asyncness, Async::Yes{..});
483+
let is_async = matches!(sig.header.asyncness, Async::Yes { .. });
484484
let returns_nothing = match &sig.decl.output {
485485
FnRetTy::Default(..) => true,
486486
FnRetTy::Ty(ty) if ty.kind.is_unit() => true,

clippy_lints/src/functions.rs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -405,13 +405,10 @@ impl<'tcx> Functions {
405405
break;
406406
}
407407
if in_comment {
408-
match line.find("*/") {
409-
Some(i) => {
410-
line = &line[i + 2..];
411-
in_comment = false;
412-
continue;
413-
},
414-
None => break,
408+
if let Some(i) = line.find("*/") {
409+
line = &line[i + 2..];
410+
in_comment = false;
411+
continue;
415412
}
416413
} else {
417414
let multi_idx = line.find("/*").unwrap_or_else(|| line.len());
@@ -423,8 +420,8 @@ impl<'tcx> Functions {
423420
in_comment = true;
424421
continue;
425422
}
426-
break;
427423
}
424+
break;
428425
}
429426
if code_in_line {
430427
line_count += 1;

clippy_lints/src/future_not_send.rs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -92,13 +92,8 @@ impl<'tcx> LateLintPass<'tcx> for FutureNotSend {
9292
|db| {
9393
cx.tcx.infer_ctxt().enter(|infcx| {
9494
for FulfillmentError { obligation, .. } in send_errors {
95-
infcx.maybe_note_obligation_cause_for_async_await(
96-
db,
97-
&obligation,
98-
);
99-
if let Trait(trait_pred, _) =
100-
obligation.predicate.skip_binders()
101-
{
95+
infcx.maybe_note_obligation_cause_for_async_await(db, &obligation);
96+
if let Trait(trait_pred, _) = obligation.predicate.skip_binders() {
10297
db.note(&format!(
10398
"`{}` doesn't implement `{}`",
10499
trait_pred.self_ty(),

clippy_lints/src/len_zero.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -222,9 +222,8 @@ fn check_impl_items(cx: &LateContext<'_>, item: &Item<'_>, impl_items: &[ImplIte
222222
let is_empty = if let Some(is_empty) = impl_items.iter().find(|i| is_named_self(cx, i, "is_empty")) {
223223
if cx.access_levels.is_exported(is_empty.id.hir_id) {
224224
return;
225-
} else {
226-
"a private"
227225
}
226+
"a private"
228227
} else {
229228
"no corresponding"
230229
};

clippy_lints/src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,7 @@ mod question_mark;
294294
mod ranges;
295295
mod redundant_clone;
296296
mod redundant_closure_call;
297+
mod redundant_else;
297298
mod redundant_field_names;
298299
mod redundant_pub_crate;
299300
mod redundant_static_lifetimes;
@@ -831,6 +832,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
831832
&ranges::REVERSED_EMPTY_RANGES,
832833
&redundant_clone::REDUNDANT_CLONE,
833834
&redundant_closure_call::REDUNDANT_CLOSURE_CALL,
835+
&redundant_else::REDUNDANT_ELSE,
834836
&redundant_field_names::REDUNDANT_FIELD_NAMES,
835837
&redundant_pub_crate::REDUNDANT_PUB_CRATE,
836838
&redundant_static_lifetimes::REDUNDANT_STATIC_LIFETIMES,
@@ -1132,6 +1134,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
11321134
store.register_early_pass(|| box items_after_statements::ItemsAfterStatements);
11331135
store.register_early_pass(|| box precedence::Precedence);
11341136
store.register_early_pass(|| box needless_continue::NeedlessContinue);
1137+
store.register_early_pass(|| box redundant_else::RedundantElse);
11351138
store.register_late_pass(|| box create_dir::CreateDir);
11361139
store.register_early_pass(|| box needless_arbitrary_self_type::NeedlessArbitrarySelfType);
11371140
store.register_early_pass(|| box redundant_static_lifetimes::RedundantStaticLifetimes);
@@ -1308,6 +1311,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
13081311
LintId::of(&pass_by_ref_or_value::TRIVIALLY_COPY_PASS_BY_REF),
13091312
LintId::of(&ranges::RANGE_MINUS_ONE),
13101313
LintId::of(&ranges::RANGE_PLUS_ONE),
1314+
LintId::of(&redundant_else::REDUNDANT_ELSE),
13111315
LintId::of(&ref_option_ref::REF_OPTION_REF),
13121316
LintId::of(&shadow::SHADOW_UNRELATED),
13131317
LintId::of(&strings::STRING_ADD_ASSIGN),

clippy_lints/src/matches.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -689,10 +689,9 @@ fn check_single_match(cx: &LateContext<'_>, ex: &Expr<'_>, arms: &[Arm<'_>], exp
689689
if stmts.len() == 1 && block_expr.is_none() || stmts.is_empty() && block_expr.is_some() {
690690
// single statement/expr "else" block, don't lint
691691
return;
692-
} else {
693-
// block with 2+ statements or 1 expr and 1+ statement
694-
Some(els)
695692
}
693+
// block with 2+ statements or 1 expr and 1+ statement
694+
Some(els)
696695
} else {
697696
// not a block, don't lint
698697
return;

clippy_lints/src/methods/unnecessary_filter_map.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,9 @@ fn check_expression<'tcx>(cx: &LateContext<'tcx>, arg_id: hir::HirId, expr: &'tc
6969
}
7070
}
7171
return (true, false);
72-
} else {
73-
// We don't know. It might do anything.
74-
return (true, true);
7572
}
73+
// We don't know. It might do anything.
74+
return (true, true);
7675
}
7776
}
7877
(true, true)

clippy_lints/src/missing_const_for_fn.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ impl<'tcx> LateLintPass<'tcx> for MissingConstForFn {
9999
let has_const_generic_params = generics
100100
.params
101101
.iter()
102-
.any(|param| matches!(param.kind, GenericParamKind::Const{ .. }));
102+
.any(|param| matches!(param.kind, GenericParamKind::Const { .. }));
103103

104104
if already_const(header) || has_const_generic_params {
105105
return;

clippy_lints/src/needless_pass_by_value.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,10 @@ impl<'tcx> LateLintPass<'tcx> for NeedlessPassByValue {
9090

9191
// Exclude non-inherent impls
9292
if let Some(Node::Item(item)) = cx.tcx.hir().find(cx.tcx.hir().get_parent_node(hir_id)) {
93-
if matches!(item.kind, ItemKind::Impl{ of_trait: Some(_), .. } |
94-
ItemKind::Trait(..))
95-
{
93+
if matches!(
94+
item.kind,
95+
ItemKind::Impl { of_trait: Some(_), .. } | ItemKind::Trait(..)
96+
) {
9697
return;
9798
}
9899
}

clippy_lints/src/non_expressive_names.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -409,11 +409,10 @@ fn levenstein_not_1(a_name: &str, b_name: &str) -> bool {
409409
if let Some(b2) = b_chars.next() {
410410
// check if there's just one character inserted
411411
return a != b2 || a_chars.ne(b_chars);
412-
} else {
413-
// tuple
414-
// ntuple
415-
return true;
416412
}
413+
// tuple
414+
// ntuple
415+
return true;
417416
}
418417
// for item in items
419418
true

clippy_lints/src/panic_in_result_fn.rs

Lines changed: 31 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,16 @@
1-
use crate::utils::{is_expn_of, is_type_diagnostic_item, return_ty, span_lint_and_then};
1+
use crate::utils::{find_macro_calls, is_type_diagnostic_item, return_ty, span_lint_and_then};
22
use rustc_hir as hir;
3-
use rustc_hir::intravisit::{self, FnKind, NestedVisitorMap, Visitor};
4-
use rustc_hir::Expr;
3+
use rustc_hir::intravisit::FnKind;
54
use rustc_lint::{LateContext, LateLintPass};
6-
use rustc_middle::hir::map::Map;
75
use rustc_session::{declare_lint_pass, declare_tool_lint};
86
use rustc_span::{sym, Span};
97

108
declare_clippy_lint! {
11-
/// **What it does:** Checks for usage of `panic!`, `unimplemented!`, `todo!` or `unreachable!` in a function of type result.
9+
/// **What it does:** Checks for usage of `panic!`, `unimplemented!`, `todo!`, `unreachable!` or assertions in a function of type result.
1210
///
13-
/// **Why is this bad?** For some codebases, it is desirable for functions of type result to return an error instead of crashing. Hence unimplemented, panic and unreachable should be avoided.
11+
/// **Why is this bad?** For some codebases, it is desirable for functions of type result to return an error instead of crashing. Hence panicking macros should be avoided.
1412
///
15-
/// **Known problems:** None.
13+
/// **Known problems:** Functions called from a function returning a `Result` may invoke a panicking macro. This is not checked.
1614
///
1715
/// **Example:**
1816
///
@@ -22,9 +20,15 @@ declare_clippy_lint! {
2220
/// panic!("error");
2321
/// }
2422
/// ```
23+
/// Use instead:
24+
/// ```rust
25+
/// fn result_without_panic() -> Result<bool, String> {
26+
/// Err(String::from("error"))
27+
/// }
28+
/// ```
2529
pub PANIC_IN_RESULT_FN,
2630
restriction,
27-
"functions of type `Result<..>` that contain `panic!()`, `todo!()` or `unreachable()` or `unimplemented()` "
31+
"functions of type `Result<..>` that contain `panic!()`, `todo!()`, `unreachable()`, `unimplemented()` or assertion"
2832
}
2933

3034
declare_lint_pass!(PanicInResultFn => [PANIC_IN_RESULT_FN]);
@@ -47,43 +51,33 @@ impl<'tcx> LateLintPass<'tcx> for PanicInResultFn {
4751
}
4852
}
4953

50-
struct FindPanicUnimplementedUnreachable {
51-
result: Vec<Span>,
52-
}
53-
54-
impl<'tcx> Visitor<'tcx> for FindPanicUnimplementedUnreachable {
55-
type Map = Map<'tcx>;
56-
57-
fn visit_expr(&mut self, expr: &'tcx Expr<'_>) {
58-
if ["unimplemented", "unreachable", "panic", "todo"]
59-
.iter()
60-
.any(|fun| is_expn_of(expr.span, fun).is_some())
61-
{
62-
self.result.push(expr.span);
63-
}
64-
// and check sub-expressions
65-
intravisit::walk_expr(self, expr);
66-
}
67-
68-
fn nested_visit_map(&mut self) -> NestedVisitorMap<Self::Map> {
69-
NestedVisitorMap::None
70-
}
71-
}
72-
7354
fn lint_impl_body<'tcx>(cx: &LateContext<'tcx>, impl_span: Span, body: &'tcx hir::Body<'tcx>) {
74-
let mut panics = FindPanicUnimplementedUnreachable { result: Vec::new() };
75-
panics.visit_expr(&body.value);
76-
if !panics.result.is_empty() {
55+
let panics = find_macro_calls(
56+
&[
57+
"unimplemented",
58+
"unreachable",
59+
"panic",
60+
"todo",
61+
"assert",
62+
"assert_eq",
63+
"assert_ne",
64+
"debug_assert",
65+
"debug_assert_eq",
66+
"debug_assert_ne",
67+
],
68+
body,
69+
);
70+
if !panics.is_empty() {
7771
span_lint_and_then(
7872
cx,
7973
PANIC_IN_RESULT_FN,
8074
impl_span,
81-
"used `unimplemented!()`, `unreachable!()`, `todo!()` or `panic!()` in a function that returns `Result`",
75+
"used `unimplemented!()`, `unreachable!()`, `todo!()`, `panic!()` or assertion in a function that returns `Result`",
8276
move |diag| {
8377
diag.help(
84-
"`unimplemented!()`, `unreachable!()`, `todo!()` or `panic!()` should not be used in a function that returns `Result` as `Result` is expected to return an error instead of crashing",
78+
"`unimplemented!()`, `unreachable!()`, `todo!()`, `panic!()` or assertions should not be used in a function that returns `Result` as `Result` is expected to return an error instead of crashing",
8579
);
86-
diag.span_note(panics.result, "return Err() instead of panicking");
80+
diag.span_note(panics, "return Err() instead of panicking");
8781
},
8882
);
8983
}

clippy_lints/src/pass_by_ref_or_value.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -244,9 +244,10 @@ impl<'tcx> LateLintPass<'tcx> for PassByRefOrValue {
244244

245245
// Exclude non-inherent impls
246246
if let Some(Node::Item(item)) = cx.tcx.hir().find(cx.tcx.hir().get_parent_node(hir_id)) {
247-
if matches!(item.kind, ItemKind::Impl{ of_trait: Some(_), .. } |
248-
ItemKind::Trait(..))
249-
{
247+
if matches!(
248+
item.kind,
249+
ItemKind::Impl { of_trait: Some(_), .. } | ItemKind::Trait(..)
250+
) {
250251
return;
251252
}
252253
}

clippy_lints/src/redundant_clone.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -390,7 +390,10 @@ impl<'tcx> mir::visit::Visitor<'tcx> for LocalUseVisitor {
390390
let local = place.local;
391391

392392
if local == self.used.0
393-
&& !matches!(ctx, PlaceContext::MutatingUse(MutatingUseContext::Drop) | PlaceContext::NonUse(_))
393+
&& !matches!(
394+
ctx,
395+
PlaceContext::MutatingUse(MutatingUseContext::Drop) | PlaceContext::NonUse(_)
396+
)
394397
{
395398
self.used.1 = true;
396399
}

0 commit comments

Comments
 (0)