Skip to content

Commit 707494e

Browse files
committed
Merge remote-tracking branch 'upstream/master' into rustup
2 parents 067bfe3 + 0c8799d commit 707494e

File tree

138 files changed

+1979
-908
lines changed

Some content is hidden

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

138 files changed

+1979
-908
lines changed

.cargo/config

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
[alias]
22
uitest = "test --test compile-test"
3-
dev = "run --target-dir clippy_dev/target --package clippy_dev --bin clippy_dev --manifest-path clippy_dev/Cargo.toml --"
4-
lintcheck = "run --target-dir lintcheck/target --package lintcheck --bin lintcheck --manifest-path lintcheck/Cargo.toml -- "
3+
dev = "run --package clippy_dev --bin clippy_dev --manifest-path clippy_dev/Cargo.toml --"
4+
lintcheck = "run --package lintcheck --bin lintcheck --manifest-path lintcheck/Cargo.toml -- "
55
collect-metadata = "test --test dogfood --features metadata-collector-lint -- run_metadata_collection_lint --ignored"
66

77
[build]
88
# -Zbinary-dep-depinfo allows us to track which rlib files to use for compiling UI tests
99
rustflags = ["-Zunstable-options", "-Zbinary-dep-depinfo"]
10+
target-dir = "target"

.github/ISSUE_TEMPLATE/blank_issue.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ about: Create a blank issue.
88
Additional labels can be added to this issue by including the following command
99
(without the space after the @ symbol):
1010
11-
`@rustbot label +<label>`
11+
@ rustbot label +<label>
1212
1313
Common labels for this issue type are:
1414
* C-an-interesting-project

.github/ISSUE_TEMPLATE/bug_report.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ LLVM version: 10.0
3636
Additional labels can be added to this issue by including the following command
3737
(without the space after the @ symbol):
3838
39-
`@rustbot label +<label>`
39+
@ rustbot label +<label>
4040
4141
Common labels for this issue type are:
4242
* `I-suggestion-causes-error`

.github/ISSUE_TEMPLATE/false_positive.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ LLVM version: 10.0
3737
Additional labels can be added to this issue by including the following command
3838
(without the space after the @ symbol):
3939
40-
`@rustbot label +<label>`
40+
@ rustbot label +<label>
4141
4242
Common labels for this issue type are:
4343
* I-suggestion-causes-error

CHANGELOG.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1050,7 +1050,7 @@ Released 2020-11-19
10501050
[#5913](https://github.com/rust-lang/rust-clippy/pull/5913)
10511051
* Add example of false positive to [`ptr_arg`] docs.
10521052
[#5885](https://github.com/rust-lang/rust-clippy/pull/5885)
1053-
* [`box_vec`], [`vec_box`] and [`borrowed_box`]: add link to the documentation of `Box`
1053+
* [`box_vec`](https://rust-lang.github.io/rust-clippy/master/index.html#box_collection), [`vec_box`] and [`borrowed_box`]: add link to the documentation of `Box`
10541054
[#6023](https://github.com/rust-lang/rust-clippy/pull/6023)
10551055

10561056
## Rust 1.47
@@ -2570,7 +2570,7 @@ Released 2018-09-13
25702570
[`bool_comparison`]: https://rust-lang.github.io/rust-clippy/master/index.html#bool_comparison
25712571
[`borrow_interior_mutable_const`]: https://rust-lang.github.io/rust-clippy/master/index.html#borrow_interior_mutable_const
25722572
[`borrowed_box`]: https://rust-lang.github.io/rust-clippy/master/index.html#borrowed_box
2573-
[`box_vec`]: https://rust-lang.github.io/rust-clippy/master/index.html#box_vec
2573+
[`box_collection`]: https://rust-lang.github.io/rust-clippy/master/index.html#box_collection
25742574
[`boxed_local`]: https://rust-lang.github.io/rust-clippy/master/index.html#boxed_local
25752575
[`branches_sharing_code`]: https://rust-lang.github.io/rust-clippy/master/index.html#branches_sharing_code
25762576
[`builtin_type_shadow`]: https://rust-lang.github.io/rust-clippy/master/index.html#builtin_type_shadow
@@ -2688,6 +2688,7 @@ Released 2018-09-13
26882688
[`if_let_some_result`]: https://rust-lang.github.io/rust-clippy/master/index.html#if_let_some_result
26892689
[`if_not_else`]: https://rust-lang.github.io/rust-clippy/master/index.html#if_not_else
26902690
[`if_same_then_else`]: https://rust-lang.github.io/rust-clippy/master/index.html#if_same_then_else
2691+
[`if_then_panic`]: https://rust-lang.github.io/rust-clippy/master/index.html#if_then_panic
26912692
[`if_then_some_else_none`]: https://rust-lang.github.io/rust-clippy/master/index.html#if_then_some_else_none
26922693
[`ifs_same_cond`]: https://rust-lang.github.io/rust-clippy/master/index.html#ifs_same_cond
26932694
[`implicit_clone`]: https://rust-lang.github.io/rust-clippy/master/index.html#implicit_clone
@@ -2722,6 +2723,7 @@ Released 2018-09-13
27222723
[`iter_count`]: https://rust-lang.github.io/rust-clippy/master/index.html#iter_count
27232724
[`iter_next_loop`]: https://rust-lang.github.io/rust-clippy/master/index.html#iter_next_loop
27242725
[`iter_next_slice`]: https://rust-lang.github.io/rust-clippy/master/index.html#iter_next_slice
2726+
[`iter_not_returning_iterator`]: https://rust-lang.github.io/rust-clippy/master/index.html#iter_not_returning_iterator
27252727
[`iter_nth`]: https://rust-lang.github.io/rust-clippy/master/index.html#iter_nth
27262728
[`iter_nth_zero`]: https://rust-lang.github.io/rust-clippy/master/index.html#iter_nth_zero
27272729
[`iter_skip_next`]: https://rust-lang.github.io/rust-clippy/master/index.html#iter_skip_next
@@ -2905,6 +2907,7 @@ Released 2018-09-13
29052907
[`reversed_empty_ranges`]: https://rust-lang.github.io/rust-clippy/master/index.html#reversed_empty_ranges
29062908
[`same_functions_in_if_condition`]: https://rust-lang.github.io/rust-clippy/master/index.html#same_functions_in_if_condition
29072909
[`same_item_push`]: https://rust-lang.github.io/rust-clippy/master/index.html#same_item_push
2910+
[`same_name_method`]: https://rust-lang.github.io/rust-clippy/master/index.html#same_name_method
29082911
[`search_is_some`]: https://rust-lang.github.io/rust-clippy/master/index.html#search_is_some
29092912
[`self_assignment`]: https://rust-lang.github.io/rust-clippy/master/index.html#self_assignment
29102913
[`self_named_constructors`]: https://rust-lang.github.io/rust-clippy/master/index.html#self_named_constructors

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ license = "MIT OR Apache-2.0"
88
keywords = ["clippy", "lint", "plugin"]
99
categories = ["development-tools", "development-tools::cargo-plugins"]
1010
build = "build.rs"
11-
edition = "2018"
11+
edition = "2021"
1212
publish = false
1313

1414
[[bin]]

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ You can choose how much Clippy is supposed to ~~annoy~~ help you by changing the
1818
| `clippy::style` | code that should be written in a more idiomatic way | **warn** |
1919
| `clippy::complexity` | code that does something simple but in a complex way | **warn** |
2020
| `clippy::perf` | code that can be written to run faster | **warn** |
21-
| `clippy::pedantic` | lints which are rather strict or might have false positives | allow |
21+
| `clippy::pedantic` | lints which are rather strict or have occasional false positives | allow |
2222
| `clippy::nursery` | new lints that are still under development | allow |
2323
| `clippy::cargo` | lints for the cargo manifest | allow |
2424

clippy_dev/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[package]
22
name = "clippy_dev"
33
version = "0.0.1"
4-
edition = "2018"
4+
edition = "2021"
55

66
[dependencies]
77
bytecount = "0.6"

clippy_dev/src/bless.rs

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
//! `bless` updates the reference files in the repo with changed output files
22
//! from the last test run.
33
4-
use std::env;
54
use std::ffi::OsStr;
65
use std::fs;
76
use std::lazy::SyncLazy;
@@ -10,17 +9,9 @@ use walkdir::WalkDir;
109

1110
use crate::clippy_project_root;
1211

13-
// NOTE: this is duplicated with tests/cargo/mod.rs What to do?
14-
pub static CARGO_TARGET_DIR: SyncLazy<PathBuf> = SyncLazy::new(|| match env::var_os("CARGO_TARGET_DIR") {
15-
Some(v) => v.into(),
16-
None => env::current_dir().unwrap().join("target"),
17-
});
18-
1912
static CLIPPY_BUILD_TIME: SyncLazy<Option<std::time::SystemTime>> = SyncLazy::new(|| {
20-
let profile = env::var("PROFILE").unwrap_or_else(|_| "debug".to_string());
21-
let mut path = PathBuf::from(&**CARGO_TARGET_DIR);
22-
path.push(profile);
23-
path.push("cargo-clippy");
13+
let mut path = std::env::current_exe().unwrap();
14+
path.set_file_name("cargo-clippy");
2415
fs::metadata(path).ok()?.modified().ok()
2516
});
2617

@@ -94,10 +85,7 @@ fn updated_since_clippy_build(path: &Path) -> Option<bool> {
9485
}
9586

9687
fn build_dir() -> PathBuf {
97-
let profile = env::var("PROFILE").unwrap_or_else(|_| "debug".to_string());
98-
let mut path = PathBuf::new();
99-
path.push(CARGO_TARGET_DIR.clone());
100-
path.push(profile);
101-
path.push("test_build_base");
88+
let mut path = std::env::current_exe().unwrap();
89+
path.set_file_name("test");
10290
path
10391
}

clippy_lints/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ repository = "https://github.com/rust-lang/rust-clippy"
66
readme = "README.md"
77
license = "MIT OR Apache-2.0"
88
keywords = ["clippy", "lint", "plugin"]
9-
edition = "2018"
9+
edition = "2021"
1010

1111
[dependencies]
1212
cargo_metadata = "0.12"

clippy_lints/src/derivable_impls.rs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,9 @@ use clippy_utils::diagnostics::span_lint_and_help;
22
use clippy_utils::{in_macro, is_automatically_derived, is_default_equivalent, remove_blocks};
33
use rustc_hir::{
44
def::{DefKind, Res},
5-
Body, Expr, ExprKind, Impl, ImplItemKind, Item, ItemKind, Node, QPath,
5+
Body, Expr, ExprKind, GenericArg, Impl, ImplItemKind, Item, ItemKind, Node, PathSegment, QPath, TyKind,
66
};
77
use rustc_lint::{LateContext, LateLintPass};
8-
use rustc_middle::ty::TypeFoldable;
98
use rustc_session::{declare_lint_pass, declare_tool_lint};
109
use rustc_span::sym;
1110

@@ -68,6 +67,7 @@ impl<'tcx> LateLintPass<'tcx> for DerivableImpls {
6867
if let ItemKind::Impl(Impl {
6968
of_trait: Some(ref trait_ref),
7069
items: [child],
70+
self_ty,
7171
..
7272
}) = item.kind;
7373
if let attrs = cx.tcx.hir().attrs(item.hir_id());
@@ -80,9 +80,18 @@ impl<'tcx> LateLintPass<'tcx> for DerivableImpls {
8080
if let ImplItemKind::Fn(_, b) = &impl_item.kind;
8181
if let Body { value: func_expr, .. } = cx.tcx.hir().body(*b);
8282
if let Some(adt_def) = cx.tcx.type_of(item.def_id).ty_adt_def();
83+
if !attrs.iter().any(|attr| attr.doc_str().is_some());
84+
if let child_attrs = cx.tcx.hir().attrs(impl_item_hir);
85+
if !child_attrs.iter().any(|attr| attr.doc_str().is_some());
8386
then {
84-
if cx.tcx.type_of(item.def_id).definitely_has_param_types_or_consts(cx.tcx) {
85-
return;
87+
if let TyKind::Path(QPath::Resolved(_, p)) = self_ty.kind {
88+
if let Some(PathSegment { args: Some(a), .. }) = p.segments.last() {
89+
for arg in a.args {
90+
if !matches!(arg, GenericArg::Lifetime(_)) {
91+
return;
92+
}
93+
}
94+
}
8695
}
8796
let should_emit = match remove_blocks(func_expr).kind {
8897
ExprKind::Tup(fields) => fields.iter().all(|e| is_default_equivalent(cx, e)),

clippy_lints/src/disallowed_method.rs

Lines changed: 48 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,44 @@
1-
use clippy_utils::diagnostics::span_lint;
1+
use clippy_utils::diagnostics::span_lint_and_then;
22
use clippy_utils::fn_def_id;
33

4-
use rustc_data_structures::fx::FxHashSet;
5-
use rustc_hir::{def::Res, def_id::DefId, Crate, Expr};
4+
use rustc_hir::{def::Res, def_id::DefIdMap, Crate, Expr};
65
use rustc_lint::{LateContext, LateLintPass};
76
use rustc_session::{declare_tool_lint, impl_lint_pass};
8-
use rustc_span::Symbol;
7+
8+
use crate::utils::conf;
99

1010
declare_clippy_lint! {
1111
/// ### What it does
1212
/// Denies the configured methods and functions in clippy.toml
1313
///
1414
/// ### Why is this bad?
15-
/// Some methods are undesirable in certain contexts,
16-
/// and it's beneficial to lint for them as needed.
15+
/// Some methods are undesirable in certain contexts, and it's beneficial to
16+
/// lint for them as needed.
1717
///
1818
/// ### Example
1919
/// An example clippy.toml configuration:
2020
/// ```toml
2121
/// # clippy.toml
22-
/// disallowed-methods = ["std::vec::Vec::leak", "std::time::Instant::now"]
22+
/// disallowed-methods = [
23+
/// # Can use a string as the path of the disallowed method.
24+
/// "std::boxed::Box::new",
25+
/// # Can also use an inline table with a `path` key.
26+
/// { path = "std::time::Instant::now" },
27+
/// # When using an inline table, can add a `reason` for why the method
28+
/// # is disallowed.
29+
/// { path = "std::vec::Vec::leak", reason = "no leaking memory" },
30+
/// ]
2331
/// ```
2432
///
2533
/// ```rust,ignore
2634
/// // Example code where clippy issues a warning
2735
/// let xs = vec![1, 2, 3, 4];
2836
/// xs.leak(); // Vec::leak is disallowed in the config.
37+
/// // The diagnostic contains the message "no leaking memory".
2938
///
3039
/// let _now = Instant::now(); // Instant::now is disallowed in the config.
40+
///
41+
/// let _box = Box::new(3); // Box::new is disallowed in the config.
3142
/// ```
3243
///
3344
/// Use instead:
@@ -43,18 +54,15 @@ declare_clippy_lint! {
4354

4455
#[derive(Clone, Debug)]
4556
pub struct DisallowedMethod {
46-
disallowed: FxHashSet<Vec<Symbol>>,
47-
def_ids: FxHashSet<(DefId, Vec<Symbol>)>,
57+
conf_disallowed: Vec<conf::DisallowedMethod>,
58+
disallowed: DefIdMap<Option<String>>,
4859
}
4960

5061
impl DisallowedMethod {
51-
pub fn new(disallowed: &FxHashSet<String>) -> Self {
62+
pub fn new(conf_disallowed: Vec<conf::DisallowedMethod>) -> Self {
5263
Self {
53-
disallowed: disallowed
54-
.iter()
55-
.map(|s| s.split("::").map(|seg| Symbol::intern(seg)).collect::<Vec<_>>())
56-
.collect(),
57-
def_ids: FxHashSet::default(),
64+
conf_disallowed,
65+
disallowed: DefIdMap::default(),
5866
}
5967
}
6068
}
@@ -63,32 +71,36 @@ impl_lint_pass!(DisallowedMethod => [DISALLOWED_METHOD]);
6371

6472
impl<'tcx> LateLintPass<'tcx> for DisallowedMethod {
6573
fn check_crate(&mut self, cx: &LateContext<'_>, _: &Crate<'_>) {
66-
for path in &self.disallowed {
67-
let segs = path.iter().map(ToString::to_string).collect::<Vec<_>>();
68-
if let Res::Def(_, id) = clippy_utils::path_to_res(cx, &segs.iter().map(String::as_str).collect::<Vec<_>>())
69-
{
70-
self.def_ids.insert((id, path.clone()));
74+
for conf in &self.conf_disallowed {
75+
let (path, reason) = match conf {
76+
conf::DisallowedMethod::Simple(path) => (path, None),
77+
conf::DisallowedMethod::WithReason { path, reason } => (
78+
path,
79+
reason.as_ref().map(|reason| format!("{} (from clippy.toml)", reason)),
80+
),
81+
};
82+
let segs: Vec<_> = path.split("::").collect();
83+
if let Res::Def(_, id) = clippy_utils::path_to_res(cx, &segs) {
84+
self.disallowed.insert(id, reason);
7185
}
7286
}
7387
}
7488

7589
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
76-
if let Some(def_id) = fn_def_id(cx, expr) {
77-
if self.def_ids.iter().any(|(id, _)| def_id == *id) {
78-
let func_path = cx.get_def_path(def_id);
79-
let func_path_string = func_path
80-
.into_iter()
81-
.map(Symbol::to_ident_string)
82-
.collect::<Vec<_>>()
83-
.join("::");
84-
85-
span_lint(
86-
cx,
87-
DISALLOWED_METHOD,
88-
expr.span,
89-
&format!("use of a disallowed method `{}`", func_path_string),
90-
);
90+
let def_id = match fn_def_id(cx, expr) {
91+
Some(def_id) => def_id,
92+
None => return,
93+
};
94+
let reason = match self.disallowed.get(&def_id) {
95+
Some(reason) => reason,
96+
None => return,
97+
};
98+
let func_path = cx.tcx.def_path_str(def_id);
99+
let msg = format!("use of a disallowed method `{}`", func_path);
100+
span_lint_and_then(cx, DISALLOWED_METHOD, expr.span, &msg, |diag| {
101+
if let Some(reason) = reason {
102+
diag.note(reason);
91103
}
92-
}
104+
});
93105
}
94106
}

clippy_lints/src/disallowed_type.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ impl DisallowedType {
4848
Self {
4949
disallowed: disallowed
5050
.iter()
51-
.map(|s| s.split("::").map(|seg| Symbol::intern(seg)).collect::<Vec<_>>())
51+
.map(|s| s.split("::").map(Symbol::intern).collect::<Vec<_>>())
5252
.collect(),
5353
def_ids: FxHashSet::default(),
5454
prim_tys: FxHashSet::default(),

clippy_lints/src/escape.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,11 @@ impl<'tcx> LateLintPass<'tcx> for BoxedLocal {
9191
if trait_item.id.hir_id() == hir_id {
9292
// be sure we have `self` parameter in this function
9393
if let AssocItemKind::Fn { has_self: true } = trait_item.kind {
94-
trait_self_ty =
95-
Some(TraitRef::identity(cx.tcx, trait_item.id.def_id.to_def_id()).self_ty().skip_binder());
94+
trait_self_ty = Some(
95+
TraitRef::identity(cx.tcx, trait_item.id.def_id.to_def_id())
96+
.self_ty()
97+
.skip_binder(),
98+
);
9699
}
97100
}
98101
}

0 commit comments

Comments
 (0)