-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
Add allow-by-default lint for unit bindings #112380
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
use crate::lints::UnitBindingsDiag; | ||
use crate::{LateLintPass, LintContext}; | ||
use rustc_hir as hir; | ||
use rustc_middle::ty::Ty; | ||
|
||
declare_lint! { | ||
/// The `unit_bindings` lint detects cases where bindings are useless because they have | ||
/// the unit type `()` as their inferred type. The lint is suppressed if the user explicitly | ||
/// annotates the let binding with the unit type `()`, or if the let binding uses an underscore | ||
/// wildcard pattern, i.e. `let _ = expr`, or if the binding is produced from macro expansions. | ||
/// | ||
/// ### Example | ||
/// | ||
/// ```rust,compile_fail | ||
/// #![deny(unit_bindings)] | ||
/// | ||
/// fn foo() { | ||
/// println!("do work"); | ||
/// } | ||
/// | ||
/// pub fn main() { | ||
/// let x = foo(); // useless binding | ||
/// } | ||
/// ``` | ||
/// | ||
/// {{produces}} | ||
/// | ||
/// ### Explanation | ||
/// | ||
/// Creating a local binding with the unit type `()` does not do much and can be a sign of a | ||
/// user error, such as in this example: | ||
/// | ||
/// ```rust,no_run | ||
/// fn main() { | ||
/// let mut x = [1, 2, 3]; | ||
/// x[0] = 5; | ||
/// let y = x.sort(); // useless binding as `sort` returns `()` and not the sorted array. | ||
/// println!("{:?}", y); // prints "()" | ||
/// } | ||
/// ``` | ||
pub UNIT_BINDINGS, | ||
Allow, | ||
"binding is useless because it has the unit `()` type" | ||
} | ||
|
||
declare_lint_pass!(UnitBindings => [UNIT_BINDINGS]); | ||
|
||
impl<'tcx> LateLintPass<'tcx> for UnitBindings { | ||
fn check_local(&mut self, cx: &crate::LateContext<'tcx>, local: &'tcx hir::Local<'tcx>) { | ||
// Suppress warning if user: | ||
// - explicitly ascribes a type to the pattern | ||
// - explicitly wrote `let pat = ();` | ||
// - explicitly wrote `let () = init;`. | ||
if !local.span.from_expansion() | ||
&& let Some(tyck_results) = cx.maybe_typeck_results() | ||
&& let Some(init) = local.init | ||
&& let init_ty = tyck_results.expr_ty(init) | ||
&& let local_ty = tyck_results.node_type(local.hir_id) | ||
&& init_ty == Ty::new_unit(cx.tcx) | ||
&& local_ty == Ty::new_unit(cx.tcx) | ||
&& local.ty.is_none() | ||
&& !matches!(init.kind, hir::ExprKind::Tup([])) | ||
&& !matches!(local.pat.kind, hir::PatKind::Tuple([], ..)) | ||
{ | ||
cx.emit_spanned_lint( | ||
UNIT_BINDINGS, | ||
local.span, | ||
UnitBindingsDiag { label: local.pat.span }, | ||
); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I understand that that is a much more rare case, but should we maybe inspect the pattern instead to find bindings inside? example:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry for the late reply. I don't really have a strong opinion on this. I would expect that this kind of code pattern to be rather rare?