Skip to content

Commit 2636682

Browse files
committed
Auto merge of #87152 - flip1995:clippyup, r=Manishearth
Update Clippy r? `@Manishearth`
2 parents 0a6c636 + e7bc411 commit 2636682

File tree

73 files changed

+2225
-607
lines changed

Some content is hidden

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

73 files changed

+2225
-607
lines changed

Diff for: src/tools/clippy/CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -2744,6 +2744,7 @@ Released 2018-09-13
27442744
[`range_step_by_zero`]: https://rust-lang.github.io/rust-clippy/master/index.html#range_step_by_zero
27452745
[`range_zip_with_len`]: https://rust-lang.github.io/rust-clippy/master/index.html#range_zip_with_len
27462746
[`rc_buffer`]: https://rust-lang.github.io/rust-clippy/master/index.html#rc_buffer
2747+
[`rc_mutex`]: https://rust-lang.github.io/rust-clippy/master/index.html#rc_mutex
27472748
[`redundant_allocation`]: https://rust-lang.github.io/rust-clippy/master/index.html#redundant_allocation
27482749
[`redundant_clone`]: https://rust-lang.github.io/rust-clippy/master/index.html#redundant_clone
27492750
[`redundant_closure`]: https://rust-lang.github.io/rust-clippy/master/index.html#redundant_closure
@@ -2797,6 +2798,7 @@ Released 2018-09-13
27972798
[`string_from_utf8_as_bytes`]: https://rust-lang.github.io/rust-clippy/master/index.html#string_from_utf8_as_bytes
27982799
[`string_lit_as_bytes`]: https://rust-lang.github.io/rust-clippy/master/index.html#string_lit_as_bytes
27992800
[`string_to_string`]: https://rust-lang.github.io/rust-clippy/master/index.html#string_to_string
2801+
[`strlen_on_c_strings`]: https://rust-lang.github.io/rust-clippy/master/index.html#strlen_on_c_strings
28002802
[`struct_excessive_bools`]: https://rust-lang.github.io/rust-clippy/master/index.html#struct_excessive_bools
28012803
[`suboptimal_flops`]: https://rust-lang.github.io/rust-clippy/master/index.html#suboptimal_flops
28022804
[`suspicious_arithmetic_impl`]: https://rust-lang.github.io/rust-clippy/master/index.html#suspicious_arithmetic_impl

Diff for: src/tools/clippy/clippy_lints/src/cargo_common_metadata.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22
33
use std::path::PathBuf;
44

5-
use clippy_utils::diagnostics::span_lint;
6-
use clippy_utils::run_lints;
5+
use clippy_utils::{diagnostics::span_lint, is_lint_allowed};
76
use rustc_hir::{hir_id::CRATE_HIR_ID, Crate};
87
use rustc_lint::{LateContext, LateLintPass};
98
use rustc_session::{declare_tool_lint, impl_lint_pass};
@@ -85,7 +84,7 @@ fn is_empty_vec(value: &[String]) -> bool {
8584

8685
impl LateLintPass<'_> for CargoCommonMetadata {
8786
fn check_crate(&mut self, cx: &LateContext<'_>, _: &Crate<'_>) {
88-
if !run_lints(cx, &[CARGO_COMMON_METADATA], CRATE_HIR_ID) {
87+
if is_lint_allowed(cx, CARGO_COMMON_METADATA, CRATE_HIR_ID) {
8988
return;
9089
}
9190

Diff for: src/tools/clippy/clippy_lints/src/copies.rs

+8-6
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use clippy_utils::diagnostics::{span_lint_and_note, span_lint_and_then};
22
use clippy_utils::source::{first_line_of_span, indent_of, reindent_multiline, snippet, snippet_opt};
33
use clippy_utils::{
44
both, count_eq, eq_expr_value, get_enclosing_block, get_parent_expr, if_sequence, in_macro, is_else_clause,
5-
run_lints, search_same, ContainsName, SpanlessEq, SpanlessHash,
5+
is_lint_allowed, search_same, ContainsName, SpanlessEq, SpanlessHash,
66
};
77
use if_chain::if_chain;
88
use rustc_data_structures::fx::FxHashSet;
@@ -120,7 +120,10 @@ declare_clippy_lint! {
120120
///
121121
/// **Why is this bad?** Duplicate code is less maintainable.
122122
///
123-
/// **Known problems:** Hopefully none.
123+
/// **Known problems:**
124+
/// * The lint doesn't check if the moved expressions modify values that are beeing used in
125+
/// the if condition. The suggestion can in that case modify the behavior of the program.
126+
/// See [rust-clippy#7452](https://github.com/rust-lang/rust-clippy/issues/7452)
124127
///
125128
/// **Example:**
126129
/// ```ignore
@@ -337,8 +340,8 @@ fn scan_block_for_eq(cx: &LateContext<'tcx>, blocks: &[&Block<'tcx>]) -> Option<
337340
if block_expr_eq;
338341
if l_stmts.len() == r_stmts.len();
339342
if l_stmts.len() == current_start_eq;
340-
if run_lints(cx, &[IF_SAME_THEN_ELSE], win[0].hir_id);
341-
if run_lints(cx, &[IF_SAME_THEN_ELSE], win[1].hir_id);
343+
if !is_lint_allowed(cx, IF_SAME_THEN_ELSE, win[0].hir_id);
344+
if !is_lint_allowed(cx, IF_SAME_THEN_ELSE, win[1].hir_id);
342345
then {
343346
span_lint_and_note(
344347
cx,
@@ -358,8 +361,7 @@ fn scan_block_for_eq(cx: &LateContext<'tcx>, blocks: &[&Block<'tcx>]) -> Option<
358361
expr_eq &= block_expr_eq;
359362
}
360363

361-
let has_expr = blocks[0].expr.is_some();
362-
if has_expr && !expr_eq {
364+
if !expr_eq {
363365
end_eq = 0;
364366
}
365367

Diff for: src/tools/clippy/clippy_lints/src/default_numeric_fallback.rs

+18-8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use clippy_utils::diagnostics::span_lint_and_sugg;
2-
use clippy_utils::source::snippet;
2+
use clippy_utils::numeric_literal;
3+
use clippy_utils::source::snippet_opt;
34
use if_chain::if_chain;
45
use rustc_ast::ast::{LitFloatType, LitIntType, LitKind};
56
use rustc_errors::Applicability;
@@ -78,16 +79,25 @@ impl<'a, 'tcx> NumericFallbackVisitor<'a, 'tcx> {
7879
if let Some(ty_bound) = self.ty_bounds.last();
7980
if matches!(lit.node,
8081
LitKind::Int(_, LitIntType::Unsuffixed) | LitKind::Float(_, LitFloatType::Unsuffixed));
81-
if !ty_bound.is_integral();
82+
if !ty_bound.is_numeric();
8283
then {
83-
let suffix = match lit_ty.kind() {
84-
ty::Int(IntTy::I32) => "i32",
85-
ty::Float(FloatTy::F64) => "f64",
84+
let (suffix, is_float) = match lit_ty.kind() {
85+
ty::Int(IntTy::I32) => ("i32", false),
86+
ty::Float(FloatTy::F64) => ("f64", true),
8687
// Default numeric fallback never results in other types.
8788
_ => return,
8889
};
8990

90-
let sugg = format!("{}_{}", snippet(self.cx, lit.span, ""), suffix);
91+
let src = if let Some(src) = snippet_opt(self.cx, lit.span) {
92+
src
93+
} else {
94+
match lit.node {
95+
LitKind::Int(src, _) => format!("{}", src),
96+
LitKind::Float(src, _) => format!("{}", src),
97+
_ => return,
98+
}
99+
};
100+
let sugg = numeric_literal::format(&src, Some(suffix), is_float);
91101
span_lint_and_sugg(
92102
self.cx,
93103
DEFAULT_NUMERIC_FALLBACK,
@@ -219,10 +229,10 @@ enum TyBound<'tcx> {
219229
}
220230

221231
impl<'tcx> TyBound<'tcx> {
222-
fn is_integral(self) -> bool {
232+
fn is_numeric(self) -> bool {
223233
match self {
224234
TyBound::Any => true,
225-
TyBound::Ty(t) => t.is_integral(),
235+
TyBound::Ty(t) => t.is_numeric(),
226236
TyBound::Nothing => false,
227237
}
228238
}

Diff for: src/tools/clippy/clippy_lints/src/dereference.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use clippy_utils::diagnostics::span_lint_and_sugg;
22
use clippy_utils::source::snippet_with_context;
33
use clippy_utils::ty::peel_mid_ty_refs;
4-
use clippy_utils::{get_parent_node, in_macro, is_allowed};
4+
use clippy_utils::{get_parent_node, in_macro, is_lint_allowed};
55
use rustc_ast::util::parser::PREC_PREFIX;
66
use rustc_errors::Applicability;
77
use rustc_hir::{BorrowKind, Expr, ExprKind, HirId, MatchSource, Mutability, Node, UnOp};
@@ -107,7 +107,7 @@ impl<'tcx> LateLintPass<'tcx> for Dereferencing {
107107

108108
match kind {
109109
RefOp::Method(target_mut)
110-
if !is_allowed(cx, EXPLICIT_DEREF_METHODS, expr.hir_id)
110+
if !is_lint_allowed(cx, EXPLICIT_DEREF_METHODS, expr.hir_id)
111111
&& is_linted_explicit_deref_position(parent, expr.hir_id, expr.span) =>
112112
{
113113
self.state = Some((

Diff for: src/tools/clippy/clippy_lints/src/derive.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use clippy_utils::diagnostics::{span_lint_and_help, span_lint_and_note, span_lint_and_then};
22
use clippy_utils::paths;
33
use clippy_utils::ty::{implements_trait, is_copy};
4-
use clippy_utils::{get_trait_def_id, is_allowed, is_automatically_derived, match_def_path};
4+
use clippy_utils::{get_trait_def_id, is_automatically_derived, is_lint_allowed, match_def_path};
55
use if_chain::if_chain;
66
use rustc_hir::def_id::DefId;
77
use rustc_hir::intravisit::{walk_expr, walk_fn, walk_item, FnKind, NestedVisitorMap, Visitor};
@@ -362,7 +362,7 @@ fn check_unsafe_derive_deserialize<'tcx>(
362362
if let ty::Adt(def, _) = ty.kind();
363363
if let Some(local_def_id) = def.did.as_local();
364364
let adt_hir_id = cx.tcx.hir().local_def_id_to_hir_id(local_def_id);
365-
if !is_allowed(cx, UNSAFE_DERIVE_DESERIALIZE, adt_hir_id);
365+
if !is_lint_allowed(cx, UNSAFE_DERIVE_DESERIALIZE, adt_hir_id);
366366
if cx.tcx.inherent_impls(def.did)
367367
.iter()
368368
.map(|imp_did| item_from_def_id(cx, *imp_did))

Diff for: src/tools/clippy/clippy_lints/src/doc.rs

+6-3
Original file line numberDiff line numberDiff line change
@@ -550,7 +550,7 @@ fn check_doc<'a, Events: Iterator<Item = (pulldown_cmark::Event<'a>, Range<usize
550550
FootnoteReference(text) | Text(text) => {
551551
let (begin, span) = get_current_span(spans, range.start);
552552
paragraph_span = paragraph_span.with_hi(span.hi());
553-
ticks_unbalanced |= text.contains('`');
553+
ticks_unbalanced |= text.contains('`') && !in_code;
554554
if Some(&text) == in_link.as_ref() || ticks_unbalanced {
555555
// Probably a link of the form `<http://example.com>`
556556
// Which are represented as a link to "http://example.com" with
@@ -595,7 +595,7 @@ fn check_code(cx: &LateContext<'_>, text: &str, edition: Edition, span: Span) {
595595
let handler = Handler::with_emitter(false, None, box emitter);
596596
let sess = ParseSess::with_span_handler(handler, sm);
597597

598-
let mut parser = match maybe_new_parser_from_source_str(&sess, filename, code.into()) {
598+
let mut parser = match maybe_new_parser_from_source_str(&sess, filename, code) {
599599
Ok(p) => p,
600600
Err(errs) => {
601601
for mut err in errs {
@@ -653,7 +653,10 @@ fn check_code(cx: &LateContext<'_>, text: &str, edition: Edition, span: Span) {
653653
// Because of the global session, we need to create a new session in a different thread with
654654
// the edition we need.
655655
let text = text.to_owned();
656-
if thread::spawn(move || has_needless_main(text, edition)).join().expect("thread::spawn failed") {
656+
if thread::spawn(move || has_needless_main(text, edition))
657+
.join()
658+
.expect("thread::spawn failed")
659+
{
657660
span_lint(cx, NEEDLESS_DOCTEST_MAIN, span, "needless `fn main` in doctest");
658661
}
659662
}

Diff for: src/tools/clippy/clippy_lints/src/eta_reduction.rs

+16-4
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
use clippy_utils::diagnostics::{span_lint_and_sugg, span_lint_and_then};
2-
use clippy_utils::higher;
32
use clippy_utils::higher::VecArgs;
43
use clippy_utils::source::snippet_opt;
54
use clippy_utils::ty::{implements_trait, type_is_unsafe_function};
5+
use clippy_utils::usage::UsedAfterExprVisitor;
6+
use clippy_utils::{get_enclosing_loop_or_closure, higher};
67
use clippy_utils::{is_adjusted, iter_input_pats};
78
use if_chain::if_chain;
89
use rustc_errors::Applicability;
910
use rustc_hir::{def_id, Expr, ExprKind, Param, PatKind, QPath};
1011
use rustc_lint::{LateContext, LateLintPass, LintContext};
1112
use rustc_middle::lint::in_external_macro;
12-
use rustc_middle::ty::{self, Ty};
13+
use rustc_middle::ty::{self, ClosureKind, Ty};
1314
use rustc_session::{declare_lint_pass, declare_tool_lint};
1415

1516
declare_clippy_lint! {
@@ -86,7 +87,7 @@ impl<'tcx> LateLintPass<'tcx> for EtaReduction {
8687
}
8788
}
8889

89-
fn check_closure(cx: &LateContext<'_>, expr: &Expr<'_>) {
90+
fn check_closure<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
9091
if let ExprKind::Closure(_, decl, eid, _, _) = expr.kind {
9192
let body = cx.tcx.hir().body(eid);
9293
let ex = &body.value;
@@ -131,7 +132,18 @@ fn check_closure(cx: &LateContext<'_>, expr: &Expr<'_>) {
131132

132133
then {
133134
span_lint_and_then(cx, REDUNDANT_CLOSURE, expr.span, "redundant closure", |diag| {
134-
if let Some(snippet) = snippet_opt(cx, caller.span) {
135+
if let Some(mut snippet) = snippet_opt(cx, caller.span) {
136+
if_chain! {
137+
if let ty::Closure(_, substs) = fn_ty.kind();
138+
if let ClosureKind::FnMut = substs.as_closure().kind();
139+
if UsedAfterExprVisitor::is_found(cx, caller)
140+
|| get_enclosing_loop_or_closure(cx.tcx, expr).is_some();
141+
142+
then {
143+
// Mutable closure is used after current expr; we cannot consume it.
144+
snippet = format!("&mut {}", snippet);
145+
}
146+
}
135147
diag.span_suggestion(
136148
expr.span,
137149
"replace the closure with the function itself",

0 commit comments

Comments
 (0)