Skip to content

Uplift the let_underscore lints from clippy into rustc. #97739

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 25 commits into from
Sep 2, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
821b32b
Add `let_underscore_drop` lint.
a2aaron May 29, 2022
ad7587f
Add `let_underscore_lock` lint.
a2aaron May 31, 2022
758a9fd
Add `let_underscore_must_use` lint.
a2aaron Jun 2, 2022
36b6309
Move let_underscore tests to their own subfolder.
a2aaron Jun 3, 2022
ae2ac3b
Allow `let_underscore_drop` and `let_underscore_must_use` by default.
a2aaron Jun 3, 2022
eba6c78
Show code suggestions in `let_undescore` lint messages.
a2aaron Jun 4, 2022
6b179e3
Set `let_underscore_lock` to Deny by default.
a2aaron Jun 4, 2022
a7e2b3e
Move local functions to outer scope.
a2aaron Jun 4, 2022
7e485bf
Move `let_underscore` tests into the `lint` subfolder.
a2aaron Jun 5, 2022
1421cff
Add `let_underscore` lint group to `GROUP_DESCRIPTIONS`.
a2aaron Jun 5, 2022
30e8adb
Use `has_attr` instead of `get_attrs` in `has_must_use_attr`
a2aaron Jun 5, 2022
e6b6678
Bail out early if the type does not has a trivial Drop implementation.
a2aaron Jun 5, 2022
6342b58
Use diagnostic items instead of hard coded paths for `let_underscore_…
a2aaron Jun 5, 2022
11663b1
Use `check-pass` instead of `run-pass`
a2aaron Jun 5, 2022
b5b5b54
Remove `let_underscore_must_use`
a2aaron Jun 5, 2022
321a598
Add diagnostic items to MutexGuard and RwLock Guards
a2aaron Jun 5, 2022
211feb1
Add `{{produces}}` tag to lint doc comments.
a2aaron Jun 5, 2022
cdf6606
Use `multipart_suggestion` to create an applicable suggestion.
a2aaron Jun 9, 2022
7237e86
Reword suggestion messages.
a2aaron Jun 11, 2022
b040666
Have the drop code suggestion not include `let _ =`
a2aaron Jun 11, 2022
8807c2d
Make `let_underscore_drop` Deny by default.
a2aaron Jun 11, 2022
a9095ff
Re-allow `let_underscore_drop` by default.
a2aaron Jun 17, 2022
a9f1b7b
Explain why let-underscoring a lock guard is incorrect.
a2aaron Aug 4, 2022
d355ec9
Fix imports.
a2aaron Aug 4, 2022
76c90c3
Use `#![warn(let_underscore_drop)]` in tests.
a2aaron Aug 5, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Use diagnostic items instead of hard coded paths for `let_underscore_…
…lock`

Using diagnostic items avoids having to update the paths if the guard
types ever get moved around for some reason. Additionally, it also greatly
simplifies the `is_sync_lock` check.
  • Loading branch information
a2aaron committed Jun 5, 2022
commit 6342b58ef0f71ca0284dced4b1d22f9726c1c74a
31 changes: 11 additions & 20 deletions compiler/rustc_lint/src/let_underscore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use rustc_errors::Applicability;
use rustc_hir as hir;
use rustc_middle::{
lint::LintDiagnosticBuilder,
ty::{self, subst::GenericArgKind, Ty},
ty::{self, Ty},
};
use rustc_span::Symbol;

Expand Down Expand Up @@ -114,12 +114,10 @@ declare_lint! {

declare_lint_pass!(LetUnderscore => [LET_UNDERSCORE_DROP, LET_UNDERSCORE_LOCK, LET_UNDERSCORE_MUST_USE]);

const SYNC_GUARD_PATHS: [&[&str]; 5] = [
&["std", "sync", "mutex", "MutexGuard"],
&["std", "sync", "rwlock", "RwLockReadGuard"],
&["std", "sync", "rwlock", "RwLockWriteGuard"],
&["parking_lot", "raw_mutex", "RawMutex"],
&["parking_lot", "raw_rwlock", "RawRwLock"],
const SYNC_GUARD_SYMBOLS: [Symbol; 3] = [
rustc_span::sym::MutexGuard,
rustc_span::sym::RwLockReadGuard,
rustc_span::sym::RwLockWriteGuard,
];

impl<'tcx> LateLintPass<'tcx> for LetUnderscore {
Expand All @@ -134,19 +132,12 @@ impl<'tcx> LateLintPass<'tcx> for LetUnderscore {
if !init_ty.needs_drop(cx.tcx, cx.param_env) {
return;
}
let is_sync_lock = init_ty.walk().any(|inner| match inner.unpack() {
GenericArgKind::Type(inner_ty) => {
SYNC_GUARD_PATHS.iter().any(|guard_path| match inner_ty.kind() {
ty::Adt(adt, _) => {
let ty_path = cx.get_def_path(adt.did());
guard_path.iter().map(|x| Symbol::intern(x)).eq(ty_path.iter().copied())
}
_ => false,
})
}

GenericArgKind::Lifetime(_) | GenericArgKind::Const(_) => false,
});
let is_sync_lock = match init_ty.kind() {
ty::Adt(adt, _) => SYNC_GUARD_SYMBOLS
.iter()
.any(|guard_symbol| cx.tcx.is_diagnostic_item(*guard_symbol, adt.did())),
Comment on lines +115 to +117
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we were ever to introduce 3rd party checks, it would be an extra arm here.

_ => false,
};
let is_must_use_ty = is_must_use_ty(cx, cx.typeck_results().expr_ty(init));
let is_must_use_func_call = is_must_use_func_call(cx, init);

Expand Down
3 changes: 3 additions & 0 deletions compiler/rustc_span/src/symbol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,7 @@ symbols! {
LinkedList,
LintPass,
Mutex,
MutexGuard,
N,
None,
Ok,
Expand Down Expand Up @@ -250,6 +251,8 @@ symbols! {
Right,
RustcDecodable,
RustcEncodable,
RwLockReadGuard,
RwLockWriteGuard,
Send,
SeqCst,
SliceIndex,
Expand Down