Skip to content

Commit 065aad2

Browse files
authored
Merge branch 'master' into feat/needless_move
2 parents d76318f + da27c97 commit 065aad2

File tree

225 files changed

+1238
-565
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

225 files changed

+1238
-565
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4946,6 +4946,7 @@ Released 2018-09-13
49464946
[`blanket_clippy_restriction_lints`]: https://rust-lang.github.io/rust-clippy/master/index.html#blanket_clippy_restriction_lints
49474947
[`block_in_if_condition_expr`]: https://rust-lang.github.io/rust-clippy/master/index.html#block_in_if_condition_expr
49484948
[`block_in_if_condition_stmt`]: https://rust-lang.github.io/rust-clippy/master/index.html#block_in_if_condition_stmt
4949+
[`blocks_in_conditions`]: https://rust-lang.github.io/rust-clippy/master/index.html#blocks_in_conditions
49494950
[`blocks_in_if_conditions`]: https://rust-lang.github.io/rust-clippy/master/index.html#blocks_in_if_conditions
49504951
[`bool_assert_comparison`]: https://rust-lang.github.io/rust-clippy/master/index.html#bool_assert_comparison
49514952
[`bool_comparison`]: https://rust-lang.github.io/rust-clippy/master/index.html#bool_comparison
@@ -5463,6 +5464,7 @@ Released 2018-09-13
54635464
[`ref_patterns`]: https://rust-lang.github.io/rust-clippy/master/index.html#ref_patterns
54645465
[`regex_macro`]: https://rust-lang.github.io/rust-clippy/master/index.html#regex_macro
54655466
[`repeat_once`]: https://rust-lang.github.io/rust-clippy/master/index.html#repeat_once
5467+
[`repeat_vec_with_capacity`]: https://rust-lang.github.io/rust-clippy/master/index.html#repeat_vec_with_capacity
54665468
[`replace_consts`]: https://rust-lang.github.io/rust-clippy/master/index.html#replace_consts
54675469
[`reserve_after_initialization`]: https://rust-lang.github.io/rust-clippy/master/index.html#reserve_after_initialization
54685470
[`rest_pat_in_fully_bound_structs`]: https://rust-lang.github.io/rust-clippy/master/index.html#rest_pat_in_fully_bound_structs
@@ -5546,6 +5548,7 @@ Released 2018-09-13
55465548
[`tabs_in_doc_comments`]: https://rust-lang.github.io/rust-clippy/master/index.html#tabs_in_doc_comments
55475549
[`temporary_assignment`]: https://rust-lang.github.io/rust-clippy/master/index.html#temporary_assignment
55485550
[`temporary_cstring_as_ptr`]: https://rust-lang.github.io/rust-clippy/master/index.html#temporary_cstring_as_ptr
5551+
[`test_attr_in_doctest`]: https://rust-lang.github.io/rust-clippy/master/index.html#test_attr_in_doctest
55495552
[`tests_outside_test_module`]: https://rust-lang.github.io/rust-clippy/master/index.html#tests_outside_test_module
55505553
[`to_digit_is_some`]: https://rust-lang.github.io/rust-clippy/master/index.html#to_digit_is_some
55515554
[`to_string_in_display`]: https://rust-lang.github.io/rust-clippy/master/index.html#to_string_in_display

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ toml = "0.7.3"
3737
walkdir = "2.3"
3838
# This is used by the `collect-metadata` alias.
3939
filetime = "0.2"
40-
itertools = "0.10.1"
40+
itertools = "0.11"
4141

4242
# UI test dependencies
4343
clippy_utils = { path = "clippy_utils" }

clippy_dev/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ edition = "2021"
77
aho-corasick = "0.7"
88
clap = "4.1.4"
99
indoc = "1.0"
10-
itertools = "0.10.1"
10+
itertools = "0.11"
1111
opener = "0.5"
1212
shell-escape = "0.1"
1313
walkdir = "2.3"

clippy_lints/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ cargo_metadata = "0.15.3"
1414
clippy_config = { path = "../clippy_config" }
1515
clippy_utils = { path = "../clippy_utils" }
1616
declare_clippy_lint = { path = "../declare_clippy_lint" }
17-
itertools = "0.10.1"
17+
itertools = "0.11"
1818
quine-mc_cluskey = "0.2"
1919
regex-syntax = "0.7"
2020
serde = { version = "1.0", features = ["derive"] }
Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
use clippy_utils::diagnostics::{span_lint, span_lint_and_sugg};
2+
use clippy_utils::source::snippet_block_with_applicability;
3+
use clippy_utils::ty::implements_trait;
4+
use clippy_utils::visitors::{for_each_expr, Descend};
5+
use clippy_utils::{get_parent_expr, higher};
6+
use core::ops::ControlFlow;
7+
use rustc_errors::Applicability;
8+
use rustc_hir::{BlockCheckMode, Expr, ExprKind, MatchSource};
9+
use rustc_lint::{LateContext, LateLintPass, LintContext};
10+
use rustc_middle::lint::in_external_macro;
11+
use rustc_session::declare_lint_pass;
12+
use rustc_span::sym;
13+
14+
declare_clippy_lint! {
15+
/// ### What it does
16+
/// Checks for `if` conditions that use blocks containing an
17+
/// expression, statements or conditions that use closures with blocks.
18+
///
19+
/// ### Why is this bad?
20+
/// Style, using blocks in the condition makes it hard to read.
21+
///
22+
/// ### Examples
23+
/// ```no_run
24+
/// # fn somefunc() -> bool { true };
25+
/// if { true } { /* ... */ }
26+
///
27+
/// if { let x = somefunc(); x } { /* ... */ }
28+
/// ```
29+
///
30+
/// Use instead:
31+
/// ```no_run
32+
/// # fn somefunc() -> bool { true };
33+
/// if true { /* ... */ }
34+
///
35+
/// let res = { let x = somefunc(); x };
36+
/// if res { /* ... */ }
37+
/// ```
38+
#[clippy::version = "1.45.0"]
39+
pub BLOCKS_IN_CONDITIONS,
40+
style,
41+
"useless or complex blocks that can be eliminated in conditions"
42+
}
43+
44+
declare_lint_pass!(BlocksInConditions => [BLOCKS_IN_CONDITIONS]);
45+
46+
const BRACED_EXPR_MESSAGE: &str = "omit braces around single expression condition";
47+
48+
impl<'tcx> LateLintPass<'tcx> for BlocksInConditions {
49+
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
50+
if in_external_macro(cx.sess(), expr.span) {
51+
return;
52+
}
53+
54+
let Some((cond, keyword, desc)) = higher::If::hir(expr)
55+
.map(|hif| (hif.cond, "if", "an `if` condition"))
56+
.or(if let ExprKind::Match(match_ex, _, MatchSource::Normal) = expr.kind {
57+
Some((match_ex, "match", "a `match` scrutinee"))
58+
} else {
59+
None
60+
})
61+
else {
62+
return;
63+
};
64+
let complex_block_message = &format!(
65+
"in {desc}, avoid complex blocks or closures with blocks; \
66+
instead, move the block or closure higher and bind it with a `let`",
67+
);
68+
69+
if let ExprKind::Block(block, _) = &cond.kind {
70+
if block.rules == BlockCheckMode::DefaultBlock {
71+
if block.stmts.is_empty() {
72+
if let Some(ex) = &block.expr {
73+
// don't dig into the expression here, just suggest that they remove
74+
// the block
75+
if expr.span.from_expansion() || ex.span.from_expansion() {
76+
return;
77+
}
78+
let mut applicability = Applicability::MachineApplicable;
79+
span_lint_and_sugg(
80+
cx,
81+
BLOCKS_IN_CONDITIONS,
82+
cond.span,
83+
BRACED_EXPR_MESSAGE,
84+
"try",
85+
snippet_block_with_applicability(cx, ex.span, "..", Some(expr.span), &mut applicability)
86+
.to_string(),
87+
applicability,
88+
);
89+
}
90+
} else {
91+
let span = block.expr.as_ref().map_or_else(|| block.stmts[0].span, |e| e.span);
92+
if span.from_expansion() || expr.span.from_expansion() {
93+
return;
94+
}
95+
// move block higher
96+
let mut applicability = Applicability::MachineApplicable;
97+
span_lint_and_sugg(
98+
cx,
99+
BLOCKS_IN_CONDITIONS,
100+
expr.span.with_hi(cond.span.hi()),
101+
complex_block_message,
102+
"try",
103+
format!(
104+
"let res = {}; {keyword} res",
105+
snippet_block_with_applicability(cx, block.span, "..", Some(expr.span), &mut applicability),
106+
),
107+
applicability,
108+
);
109+
}
110+
}
111+
} else {
112+
let _: Option<!> = for_each_expr(cond, |e| {
113+
if let ExprKind::Closure(closure) = e.kind {
114+
// do not lint if the closure is called using an iterator (see #1141)
115+
if let Some(parent) = get_parent_expr(cx, e)
116+
&& let ExprKind::MethodCall(_, self_arg, _, _) = &parent.kind
117+
&& let caller = cx.typeck_results().expr_ty(self_arg)
118+
&& let Some(iter_id) = cx.tcx.get_diagnostic_item(sym::Iterator)
119+
&& implements_trait(cx, caller, iter_id, &[])
120+
{
121+
return ControlFlow::Continue(Descend::No);
122+
}
123+
124+
let body = cx.tcx.hir().body(closure.body);
125+
let ex = &body.value;
126+
if let ExprKind::Block(block, _) = ex.kind {
127+
if !body.value.span.from_expansion() && !block.stmts.is_empty() {
128+
span_lint(cx, BLOCKS_IN_CONDITIONS, ex.span, complex_block_message);
129+
return ControlFlow::Continue(Descend::No);
130+
}
131+
}
132+
}
133+
ControlFlow::Continue(Descend::Yes)
134+
});
135+
}
136+
}
137+
}

clippy_lints/src/blocks_in_if_conditions.rs

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

clippy_lints/src/booleans.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -424,8 +424,9 @@ impl<'a, 'tcx> NonminimalBoolVisitor<'a, 'tcx> {
424424
improvements.push(suggestion);
425425
}
426426
}
427-
let nonminimal_bool_lint = |suggestions: Vec<_>| {
427+
let nonminimal_bool_lint = |mut suggestions: Vec<_>| {
428428
if self.cx.tcx.lint_level_at_node(NONMINIMAL_BOOL, e.hir_id).0 != Level::Allow {
429+
suggestions.sort();
429430
span_lint_hir_and_then(
430431
self.cx,
431432
NONMINIMAL_BOOL,

clippy_lints/src/declared_lints.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ pub(crate) static LINTS: &[&crate::LintInfo] = &[
6363
crate::await_holding_invalid::AWAIT_HOLDING_INVALID_TYPE_INFO,
6464
crate::await_holding_invalid::AWAIT_HOLDING_LOCK_INFO,
6565
crate::await_holding_invalid::AWAIT_HOLDING_REFCELL_REF_INFO,
66-
crate::blocks_in_if_conditions::BLOCKS_IN_IF_CONDITIONS_INFO,
66+
crate::blocks_in_conditions::BLOCKS_IN_CONDITIONS_INFO,
6767
crate::bool_assert_comparison::BOOL_ASSERT_COMPARISON_INFO,
6868
crate::bool_to_int_with_if::BOOL_TO_INT_WITH_IF_INFO,
6969
crate::booleans::NONMINIMAL_BOOL_INFO,
@@ -140,6 +140,7 @@ pub(crate) static LINTS: &[&crate::LintInfo] = &[
140140
crate::doc::MISSING_SAFETY_DOC_INFO,
141141
crate::doc::NEEDLESS_DOCTEST_MAIN_INFO,
142142
crate::doc::SUSPICIOUS_DOC_COMMENTS_INFO,
143+
crate::doc::TEST_ATTR_IN_DOCTEST_INFO,
143144
crate::doc::UNNECESSARY_SAFETY_DOC_INFO,
144145
crate::double_parens::DOUBLE_PARENS_INFO,
145146
crate::drop_forget_ref::DROP_NON_DROP_INFO,
@@ -598,6 +599,7 @@ pub(crate) static LINTS: &[&crate::LintInfo] = &[
598599
crate::reference::DEREF_ADDROF_INFO,
599600
crate::regex::INVALID_REGEX_INFO,
600601
crate::regex::TRIVIAL_REGEX_INFO,
602+
crate::repeat_vec_with_capacity::REPEAT_VEC_WITH_CAPACITY_INFO,
601603
crate::reserve_after_initialization::RESERVE_AFTER_INITIALIZATION_INFO,
602604
crate::return_self_not_must_use::RETURN_SELF_NOT_MUST_USE_INFO,
603605
crate::returns::LET_AND_RETURN_INFO,

clippy_lints/src/dereference.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -771,7 +771,7 @@ impl TyCoercionStability {
771771
DefinedTy::Mir(ty) => Self::for_mir_ty(
772772
cx.tcx,
773773
ty.param_env,
774-
cx.tcx.erase_late_bound_regions(ty.value),
774+
cx.tcx.instantiate_bound_regions_with_erased(ty.value),
775775
for_return,
776776
),
777777
}

0 commit comments

Comments
 (0)