Skip to content

Commit 9d78d1d

Browse files
committed
Auto merge of #78774 - flip1995:clippyup, r=Manishearth
Update Clippy Biweekly Clippy update r? `@Manishearth`
2 parents b1d9f31 + 46c714f commit 9d78d1d

File tree

150 files changed

+2839
-675
lines changed

Some content is hidden

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

150 files changed

+2839
-675
lines changed

src/tools/clippy/.github/driver.sh

+3-3
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,16 @@ unset CARGO_MANIFEST_DIR
2424
# FIXME: How to match the clippy invocation in compile-test.rs?
2525
./target/debug/clippy-driver -Dwarnings -Aunused -Zui-testing --emit metadata --crate-type bin tests/ui/double_neg.rs 2>double_neg.stderr && exit 1
2626
sed -e "s,tests/ui,\$DIR," -e "/= help/d" double_neg.stderr >normalized.stderr
27-
diff normalized.stderr tests/ui/double_neg.stderr
27+
diff -u normalized.stderr tests/ui/double_neg.stderr
2828

2929
# make sure "clippy-driver --rustc --arg" and "rustc --arg" behave the same
3030
SYSROOT=$(rustc --print sysroot)
31-
diff <(LD_LIBRARY_PATH=${SYSROOT}/lib ./target/debug/clippy-driver --rustc --version --verbose) <(rustc --version --verbose)
31+
diff -u <(LD_LIBRARY_PATH=${SYSROOT}/lib ./target/debug/clippy-driver --rustc --version --verbose) <(rustc --version --verbose)
3232

3333
echo "fn main() {}" >target/driver_test.rs
3434
# we can't run 2 rustcs on the same file at the same time
3535
CLIPPY=$(LD_LIBRARY_PATH=${SYSROOT}/lib ./target/debug/clippy-driver ./target/driver_test.rs --rustc)
3636
RUSTC=$(rustc ./target/driver_test.rs)
37-
diff <($CLIPPY) <($RUSTC)
37+
diff -u <($CLIPPY) <($RUSTC)
3838

3939
# TODO: CLIPPY_CONF_DIR / CARGO_MANIFEST_DIR

src/tools/clippy/CHANGELOG.md

+8-2
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ Current beta, release 2020-11-19
2222
* [`map_err_ignore`] [#5998](https://github.com/rust-lang/rust-clippy/pull/5998)
2323
* [`rc_buffer`] [#6044](https://github.com/rust-lang/rust-clippy/pull/6044)
2424
* [`to_string_in_display`] [#5831](https://github.com/rust-lang/rust-clippy/pull/5831)
25-
* [`single_char_push_str`] [#5881](https://github.com/rust-lang/rust-clippy/pull/5881)
25+
* `single_char_push_str` [#5881](https://github.com/rust-lang/rust-clippy/pull/5881)
2626

2727
### Moves and Deprecations
2828

@@ -1665,6 +1665,7 @@ Released 2018-09-13
16651665
[`cognitive_complexity`]: https://rust-lang.github.io/rust-clippy/master/index.html#cognitive_complexity
16661666
[`collapsible_if`]: https://rust-lang.github.io/rust-clippy/master/index.html#collapsible_if
16671667
[`comparison_chain`]: https://rust-lang.github.io/rust-clippy/master/index.html#comparison_chain
1668+
[`comparison_to_empty`]: https://rust-lang.github.io/rust-clippy/master/index.html#comparison_to_empty
16681669
[`copy_iterator`]: https://rust-lang.github.io/rust-clippy/master/index.html#copy_iterator
16691670
[`create_dir`]: https://rust-lang.github.io/rust-clippy/master/index.html#create_dir
16701671
[`crosspointer_transmute`]: https://rust-lang.github.io/rust-clippy/master/index.html#crosspointer_transmute
@@ -1713,6 +1714,7 @@ Released 2018-09-13
17131714
[`extend_from_slice`]: https://rust-lang.github.io/rust-clippy/master/index.html#extend_from_slice
17141715
[`extra_unused_lifetimes`]: https://rust-lang.github.io/rust-clippy/master/index.html#extra_unused_lifetimes
17151716
[`fallible_impl_from`]: https://rust-lang.github.io/rust-clippy/master/index.html#fallible_impl_from
1717+
[`field_reassign_with_default`]: https://rust-lang.github.io/rust-clippy/master/index.html#field_reassign_with_default
17161718
[`filetype_is_file`]: https://rust-lang.github.io/rust-clippy/master/index.html#filetype_is_file
17171719
[`filter_map`]: https://rust-lang.github.io/rust-clippy/master/index.html#filter_map
17181720
[`filter_map_next`]: https://rust-lang.github.io/rust-clippy/master/index.html#filter_map_next
@@ -1731,6 +1733,7 @@ Released 2018-09-13
17311733
[`for_loops_over_fallibles`]: https://rust-lang.github.io/rust-clippy/master/index.html#for_loops_over_fallibles
17321734
[`forget_copy`]: https://rust-lang.github.io/rust-clippy/master/index.html#forget_copy
17331735
[`forget_ref`]: https://rust-lang.github.io/rust-clippy/master/index.html#forget_ref
1736+
[`from_iter_instead_of_collect`]: https://rust-lang.github.io/rust-clippy/master/index.html#from_iter_instead_of_collect
17341737
[`future_not_send`]: https://rust-lang.github.io/rust-clippy/master/index.html#future_not_send
17351738
[`get_last_with_len`]: https://rust-lang.github.io/rust-clippy/master/index.html#get_last_with_len
17361739
[`get_unwrap`]: https://rust-lang.github.io/rust-clippy/master/index.html#get_unwrap
@@ -1795,13 +1798,15 @@ Released 2018-09-13
17951798
[`manual_async_fn`]: https://rust-lang.github.io/rust-clippy/master/index.html#manual_async_fn
17961799
[`manual_memcpy`]: https://rust-lang.github.io/rust-clippy/master/index.html#manual_memcpy
17971800
[`manual_non_exhaustive`]: https://rust-lang.github.io/rust-clippy/master/index.html#manual_non_exhaustive
1801+
[`manual_ok_or`]: https://rust-lang.github.io/rust-clippy/master/index.html#manual_ok_or
17981802
[`manual_range_contains`]: https://rust-lang.github.io/rust-clippy/master/index.html#manual_range_contains
17991803
[`manual_saturating_arithmetic`]: https://rust-lang.github.io/rust-clippy/master/index.html#manual_saturating_arithmetic
18001804
[`manual_strip`]: https://rust-lang.github.io/rust-clippy/master/index.html#manual_strip
18011805
[`manual_swap`]: https://rust-lang.github.io/rust-clippy/master/index.html#manual_swap
18021806
[`manual_unwrap_or`]: https://rust-lang.github.io/rust-clippy/master/index.html#manual_unwrap_or
18031807
[`many_single_char_names`]: https://rust-lang.github.io/rust-clippy/master/index.html#many_single_char_names
18041808
[`map_clone`]: https://rust-lang.github.io/rust-clippy/master/index.html#map_clone
1809+
[`map_collect_result_unit`]: https://rust-lang.github.io/rust-clippy/master/index.html#map_collect_result_unit
18051810
[`map_entry`]: https://rust-lang.github.io/rust-clippy/master/index.html#map_entry
18061811
[`map_err_ignore`]: https://rust-lang.github.io/rust-clippy/master/index.html#map_err_ignore
18071812
[`map_flatten`]: https://rust-lang.github.io/rust-clippy/master/index.html#map_flatten
@@ -1917,6 +1922,7 @@ Released 2018-09-13
19171922
[`redundant_pub_crate`]: https://rust-lang.github.io/rust-clippy/master/index.html#redundant_pub_crate
19181923
[`redundant_static_lifetimes`]: https://rust-lang.github.io/rust-clippy/master/index.html#redundant_static_lifetimes
19191924
[`ref_in_deref`]: https://rust-lang.github.io/rust-clippy/master/index.html#ref_in_deref
1925+
[`ref_option_ref`]: https://rust-lang.github.io/rust-clippy/master/index.html#ref_option_ref
19201926
[`regex_macro`]: https://rust-lang.github.io/rust-clippy/master/index.html#regex_macro
19211927
[`repeat_once`]: https://rust-lang.github.io/rust-clippy/master/index.html#repeat_once
19221928
[`replace_consts`]: https://rust-lang.github.io/rust-clippy/master/index.html#replace_consts
@@ -1937,8 +1943,8 @@ Released 2018-09-13
19371943
[`should_assert_eq`]: https://rust-lang.github.io/rust-clippy/master/index.html#should_assert_eq
19381944
[`should_implement_trait`]: https://rust-lang.github.io/rust-clippy/master/index.html#should_implement_trait
19391945
[`similar_names`]: https://rust-lang.github.io/rust-clippy/master/index.html#similar_names
1946+
[`single_char_add_str`]: https://rust-lang.github.io/rust-clippy/master/index.html#single_char_add_str
19401947
[`single_char_pattern`]: https://rust-lang.github.io/rust-clippy/master/index.html#single_char_pattern
1941-
[`single_char_push_str`]: https://rust-lang.github.io/rust-clippy/master/index.html#single_char_push_str
19421948
[`single_component_path_imports`]: https://rust-lang.github.io/rust-clippy/master/index.html#single_component_path_imports
19431949
[`single_element_loop`]: https://rust-lang.github.io/rust-clippy/master/index.html#single_element_loop
19441950
[`single_match`]: https://rust-lang.github.io/rust-clippy/master/index.html#single_match

src/tools/clippy/CONTRIBUTING.md

+4-3
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,10 @@ To figure out how this syntax structure is encoded in the AST, it is recommended
6363
Usually the lint will end up to be a nested series of matches and ifs, [like so][deep-nesting].
6464
But we can make it nest-less by using [if_chain] macro, [like this][nest-less].
6565

66-
[`E-medium`] issues are generally pretty easy too, though it's recommended you work on an E-easy issue first.
67-
They are mostly classified as [`E-medium`], since they might be somewhat involved code wise,
68-
but not difficult per-se.
66+
[`E-medium`] issues are generally pretty easy too, though it's recommended you work on an [`good first issue`]
67+
first. Sometimes they are only somewhat involved code wise, but not difficult per-se.
68+
Note that [`E-medium`] issues may require some knowledge of Clippy internals or some
69+
debugging to find the actual problem behind the issue.
6970

7071
[`T-middle`] issues can be more involved and require verifying types. The [`ty`] module contains a
7172
lot of methods that are useful, though one of the most useful would be `expr_ty` (gives the type of

src/tools/clippy/README.md

+9-6
Original file line numberDiff line numberDiff line change
@@ -167,18 +167,21 @@ You can add options to your code to `allow`/`warn`/`deny` Clippy lints:
167167

168168
* `allow`/`warn`/`deny` can be limited to a single function or module using `#[allow(...)]`, etc.
169169

170-
Note: `deny` produces errors instead of warnings.
170+
Note: `allow` means to suppress the lint for your code. With `warn` the lint
171+
will only emit a warning, while with `deny` the lint will emit an error, when
172+
triggering for your code. An error causes clippy to exit with an error code, so
173+
is useful in scripts like CI/CD.
171174

172-
If you do not want to include your lint levels in your code, you can globally enable/disable lints
173-
by passing extra flags to Clippy during the run:
175+
If you do not want to include your lint levels in your code, you can globally
176+
enable/disable lints by passing extra flags to Clippy during the run:
174177

175-
To disable `lint_name`, run
178+
To allow `lint_name`, run
176179

177180
```terminal
178181
cargo clippy -- -A clippy::lint_name
179182
```
180183

181-
And to enable `lint_name`, run
184+
And to warn on `lint_name`, run
182185

183186
```terminal
184187
cargo clippy -- -W clippy::lint_name
@@ -190,7 +193,7 @@ can run Clippy with warnings for all lints enabled:
190193
cargo clippy -- -W clippy::pedantic
191194
```
192195

193-
If you care only about a single lint, you can allow all others and then explicitly reenable
196+
If you care only about a single lint, you can allow all others and then explicitly warn on
194197
the lint(s) you are interested in:
195198
```terminal
196199
cargo clippy -- -A clippy::all -W clippy::useless_format -W clippy::...

src/tools/clippy/clippy_dev/src/ra_setup.rs

+10-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use std::path::PathBuf;
1111
// code. See https://github.com/rust-analyzer/rust-analyzer/issues/3517 and https://github.com/rust-lang/rust-clippy/issues/5514 for details
1212

1313
pub fn run(rustc_path: Option<&str>) {
14-
// we can unwrap here because the arg is required here
14+
// we can unwrap here because the arg is required by clap
1515
let rustc_path = PathBuf::from(rustc_path.unwrap());
1616
assert!(rustc_path.is_dir(), "path is not a directory");
1717
let rustc_source_basedir = rustc_path.join("compiler");
@@ -49,6 +49,15 @@ fn inject_deps_into_manifest(
4949
cargo_toml: &str,
5050
lib_rs: &str,
5151
) -> std::io::Result<()> {
52+
// do not inject deps if we have aleady done so
53+
if cargo_toml.contains("[target.'cfg(NOT_A_PLATFORM)'.dependencies]") {
54+
eprintln!(
55+
"cargo dev ra-setup: warning: deps already found inside {}, doing nothing.",
56+
manifest_path
57+
);
58+
return Ok(());
59+
}
60+
5261
let extern_crates = lib_rs
5362
.lines()
5463
// get the deps

src/tools/clippy/clippy_lints/src/arithmetic.rs

+22-3
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,28 @@ impl<'tcx> LateLintPass<'tcx> for Arithmetic {
8888

8989
let (l_ty, r_ty) = (cx.typeck_results().expr_ty(l), cx.typeck_results().expr_ty(r));
9090
if l_ty.peel_refs().is_integral() && r_ty.peel_refs().is_integral() {
91-
span_lint(cx, INTEGER_ARITHMETIC, expr.span, "integer arithmetic detected");
92-
self.expr_span = Some(expr.span);
93-
} else if l_ty.peel_refs().is_floating_point() && r_ty.peel_refs().is_floating_point() {
91+
match op.node {
92+
hir::BinOpKind::Div | hir::BinOpKind::Rem => match &r.kind {
93+
hir::ExprKind::Lit(_lit) => (),
94+
hir::ExprKind::Unary(hir::UnOp::UnNeg, expr) => {
95+
if let hir::ExprKind::Lit(lit) = &expr.kind {
96+
if let rustc_ast::ast::LitKind::Int(1, _) = lit.node {
97+
span_lint(cx, INTEGER_ARITHMETIC, expr.span, "integer arithmetic detected");
98+
self.expr_span = Some(expr.span);
99+
}
100+
}
101+
},
102+
_ => {
103+
span_lint(cx, INTEGER_ARITHMETIC, expr.span, "integer arithmetic detected");
104+
self.expr_span = Some(expr.span);
105+
},
106+
},
107+
_ => {
108+
span_lint(cx, INTEGER_ARITHMETIC, expr.span, "integer arithmetic detected");
109+
self.expr_span = Some(expr.span);
110+
},
111+
}
112+
} else if r_ty.peel_refs().is_floating_point() && r_ty.peel_refs().is_floating_point() {
94113
span_lint(cx, FLOAT_ARITHMETIC, expr.span, "floating-point arithmetic detected");
95114
self.expr_span = Some(expr.span);
96115
}

src/tools/clippy/clippy_lints/src/attrs.rs

+11-11
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ use rustc_middle::lint::in_external_macro;
1616
use rustc_middle::ty;
1717
use rustc_session::{declare_lint_pass, declare_tool_lint};
1818
use rustc_span::source_map::Span;
19+
use rustc_span::sym;
1920
use rustc_span::symbol::{Symbol, SymbolStr};
2021
use semver::Version;
2122

@@ -286,14 +287,14 @@ impl<'tcx> LateLintPass<'tcx> for Attributes {
286287
},
287288
_ => {},
288289
}
289-
if items.is_empty() || !attr.has_name(sym!(deprecated)) {
290+
if items.is_empty() || !attr.has_name(sym::deprecated) {
290291
return;
291292
}
292293
for item in items {
293294
if_chain! {
294295
if let NestedMetaItem::MetaItem(mi) = &item;
295296
if let MetaItemKind::NameValue(lit) = &mi.kind;
296-
if mi.has_name(sym!(since));
297+
if mi.has_name(sym::since);
297298
then {
298299
check_semver(cx, item.span(), lit);
299300
}
@@ -309,7 +310,7 @@ impl<'tcx> LateLintPass<'tcx> for Attributes {
309310
}
310311
match item.kind {
311312
ItemKind::ExternCrate(..) | ItemKind::Use(..) => {
312-
let skip_unused_imports = item.attrs.iter().any(|attr| attr.has_name(sym!(macro_use)));
313+
let skip_unused_imports = item.attrs.iter().any(|attr| attr.has_name(sym::macro_use));
313314

314315
for attr in item.attrs {
315316
if in_external_macro(cx.sess(), attr.span) {
@@ -326,7 +327,7 @@ impl<'tcx> LateLintPass<'tcx> for Attributes {
326327
match item.kind {
327328
ItemKind::Use(..) => {
328329
if is_word(lint, sym!(unused_imports))
329-
|| is_word(lint, sym!(deprecated))
330+
|| is_word(lint, sym::deprecated)
330331
|| is_word(lint, sym!(unreachable_pub))
331332
|| is_word(lint, sym!(unused))
332333
|| extract_clippy_lint(lint)
@@ -411,8 +412,7 @@ fn check_clippy_lint_names(cx: &LateContext<'_>, ident: &str, items: &[NestedMet
411412
let lint_store = cx.lints();
412413
for lint in items {
413414
if let Some(lint_name) = extract_clippy_lint(lint) {
414-
if let CheckLintNameResult::Tool(Err((None, _))) =
415-
lint_store.check_lint_name(&lint_name, Some(sym!(clippy)))
415+
if let CheckLintNameResult::Tool(Err((None, _))) = lint_store.check_lint_name(&lint_name, Some(sym::clippy))
416416
{
417417
span_lint_and_then(
418418
cx,
@@ -529,10 +529,10 @@ fn check_attrs(cx: &LateContext<'_>, span: Span, name: Symbol, attrs: &[Attribut
529529

530530
for attr in attrs {
531531
if let Some(values) = attr.meta_item_list() {
532-
if values.len() != 1 || !attr.has_name(sym!(inline)) {
532+
if values.len() != 1 || !attr.has_name(sym::inline) {
533533
continue;
534534
}
535-
if is_word(&values[0], sym!(always)) {
535+
if is_word(&values[0], sym::always) {
536536
span_lint(
537537
cx,
538538
INLINE_ALWAYS,
@@ -623,12 +623,12 @@ fn check_empty_line_after_outer_attr(cx: &EarlyContext<'_>, item: &rustc_ast::It
623623
fn check_deprecated_cfg_attr(cx: &EarlyContext<'_>, attr: &Attribute) {
624624
if_chain! {
625625
// check cfg_attr
626-
if attr.has_name(sym!(cfg_attr));
626+
if attr.has_name(sym::cfg_attr);
627627
if let Some(items) = attr.meta_item_list();
628628
if items.len() == 2;
629629
// check for `rustfmt`
630630
if let Some(feature_item) = items[0].meta_item();
631-
if feature_item.has_name(sym!(rustfmt));
631+
if feature_item.has_name(sym::rustfmt);
632632
// check for `rustfmt_skip` and `rustfmt::skip`
633633
if let Some(skip_item) = &items[1].meta_item();
634634
if skip_item.has_name(sym!(rustfmt_skip)) ||
@@ -690,7 +690,7 @@ fn check_mismatched_target_os(cx: &EarlyContext<'_>, attr: &Attribute) {
690690
}
691691

692692
if_chain! {
693-
if attr.has_name(sym!(cfg));
693+
if attr.has_name(sym::cfg);
694694
if let Some(list) = attr.meta_item_list();
695695
let mismatched = find_mismatched_target_os(&list);
696696
if !mismatched.is_empty();

src/tools/clippy/clippy_lints/src/booleans.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ use rustc_lint::{LateContext, LateLintPass};
1111
use rustc_middle::hir::map::Map;
1212
use rustc_session::{declare_lint_pass, declare_tool_lint};
1313
use rustc_span::source_map::Span;
14+
use rustc_span::sym;
1415

1516
declare_clippy_lint! {
1617
/// **What it does:** Checks for boolean expressions that can be written more
@@ -253,8 +254,8 @@ fn simplify_not(cx: &LateContext<'_>, expr: &Expr<'_>) -> Option<String> {
253254
},
254255
ExprKind::MethodCall(path, _, args, _) if args.len() == 1 => {
255256
let type_of_receiver = cx.typeck_results().expr_ty(&args[0]);
256-
if !is_type_diagnostic_item(cx, type_of_receiver, sym!(option_type))
257-
&& !is_type_diagnostic_item(cx, type_of_receiver, sym!(result_type))
257+
if !is_type_diagnostic_item(cx, type_of_receiver, sym::option_type)
258+
&& !is_type_diagnostic_item(cx, type_of_receiver, sym::result_type)
258259
{
259260
return None;
260261
}

src/tools/clippy/clippy_lints/src/bytecount.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use rustc_hir::{BinOpKind, BorrowKind, Expr, ExprKind, UnOp};
88
use rustc_lint::{LateContext, LateLintPass};
99
use rustc_middle::ty;
1010
use rustc_session::{declare_lint_pass, declare_tool_lint};
11+
use rustc_span::sym;
1112
use rustc_span::Symbol;
1213

1314
declare_clippy_lint! {
@@ -68,7 +69,7 @@ impl<'tcx> LateLintPass<'tcx> for ByteCount {
6869
let haystack = if let ExprKind::MethodCall(ref path, _, ref args, _) =
6970
filter_args[0].kind {
7071
let p = path.ident.name;
71-
if (p == sym!(iter) || p == sym!(iter_mut)) && args.len() == 1 {
72+
if (p == sym::iter || p == sym!(iter_mut)) && args.len() == 1 {
7273
&args[0]
7374
} else {
7475
&filter_args[0]

src/tools/clippy/clippy_lints/src/cognitive_complexity.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use rustc_lint::{LateContext, LateLintPass, LintContext};
77
use rustc_middle::hir::map::Map;
88
use rustc_session::{declare_tool_lint, impl_lint_pass};
99
use rustc_span::source_map::Span;
10-
use rustc_span::BytePos;
10+
use rustc_span::{sym, BytePos};
1111

1212
use crate::utils::{is_type_diagnostic_item, snippet_opt, span_lint_and_help, LimitStack};
1313

@@ -61,7 +61,7 @@ impl CognitiveComplexity {
6161
helper.visit_expr(expr);
6262
let CCHelper { cc, returns } = helper;
6363
let ret_ty = cx.typeck_results().node_type(expr.hir_id);
64-
let ret_adjust = if is_type_diagnostic_item(cx, ret_ty, sym!(result_type)) {
64+
let ret_adjust = if is_type_diagnostic_item(cx, ret_ty, sym::result_type) {
6565
returns
6666
} else {
6767
#[allow(clippy::integer_division)]
@@ -123,7 +123,7 @@ impl<'tcx> LateLintPass<'tcx> for CognitiveComplexity {
123123
hir_id: HirId,
124124
) {
125125
let def_id = cx.tcx.hir().local_def_id(hir_id);
126-
if !cx.tcx.has_attr(def_id.to_def_id(), sym!(test)) {
126+
if !cx.tcx.has_attr(def_id.to_def_id(), sym::test) {
127127
self.check(cx, kind, decl, body, span);
128128
}
129129
}

0 commit comments

Comments
 (0)