Skip to content
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 a new lint: deref coercions #14059

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5515,6 +5515,7 @@ Released 2018-09-13
[`deprecated_semver`]: https://rust-lang.github.io/rust-clippy/master/index.html#deprecated_semver
[`deref_addrof`]: https://rust-lang.github.io/rust-clippy/master/index.html#deref_addrof
[`deref_by_slicing`]: https://rust-lang.github.io/rust-clippy/master/index.html#deref_by_slicing
[`deref_coercions`]: https://rust-lang.github.io/rust-clippy/master/index.html#deref_coercions
[`derivable_impls`]: https://rust-lang.github.io/rust-clippy/master/index.html#derivable_impls
[`derive_hash_xor_eq`]: https://rust-lang.github.io/rust-clippy/master/index.html#derive_hash_xor_eq
[`derive_ord_xor_partial_ord`]: https://rust-lang.github.io/rust-clippy/master/index.html#derive_ord_xor_partial_ord
Expand Down
1 change: 1 addition & 0 deletions clippy_lints/src/declared_lints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ pub static LINTS: &[&crate::LintInfo] = &[
crate::default_instead_of_iter_empty::DEFAULT_INSTEAD_OF_ITER_EMPTY_INFO,
crate::default_numeric_fallback::DEFAULT_NUMERIC_FALLBACK_INFO,
crate::default_union_representation::DEFAULT_UNION_REPRESENTATION_INFO,
crate::deref_coercions::DEREF_COERCIONS_INFO,
crate::dereference::EXPLICIT_AUTO_DEREF_INFO,
crate::dereference::EXPLICIT_DEREF_METHODS_INFO,
crate::dereference::NEEDLESS_BORROW_INFO,
Expand Down
51 changes: 51 additions & 0 deletions clippy_lints/src/deref_coercions.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
use clippy_utils::diagnostics::span_lint_and_help;
use rustc_hir::Expr;
use rustc_lint::{LateContext, LateLintPass};
use rustc_middle::ty::adjustment::Adjust;
use rustc_session::declare_lint_pass;

declare_clippy_lint! {
/// ### What it does
/// Checks for expressions that use deref coercion.
///
/// ### Why is this bad?
/// Implicit deref coercion could result in confusing behavior when writing unsafe code.
///
/// ### Example
/// ```no_run
/// let x = &Box::new(true);
/// let y: &bool = x;
/// ```
/// Use instead:
/// ```no_run
/// use std::ops::Deref;
/// let x = &Box::new(true);
/// let y: &bool = x.deref();
/// ```
#[clippy::version = "1.86.0"]
pub DEREF_COERCIONS,
restriction,
"using deref coercion"
}

declare_lint_pass!(DerefCoercions => [DEREF_COERCIONS]);

impl LateLintPass<'_> for DerefCoercions {
fn check_expr(&mut self, cx: &LateContext<'_>, expr: &Expr<'_>) {
let source = cx.typeck_results().expr_ty(expr).peel_refs();
for adjustment in cx.typeck_results().expr_adjustments(expr) {
if let Adjust::Deref(_) = adjustment.kind
&& adjustment.target.peel_refs() != source
{
span_lint_and_help(
cx,
DEREF_COERCIONS,
expr.span,
"implicit deref coercion",
None,
"consider using `deref() or deref_mut()`",
);
}
}
}
}
2 changes: 2 additions & 0 deletions clippy_lints/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ mod default_constructed_unit_structs;
mod default_instead_of_iter_empty;
mod default_numeric_fallback;
mod default_union_representation;
mod deref_coercions;
mod dereference;
mod derivable_impls;
mod derive;
Expand Down Expand Up @@ -976,5 +977,6 @@ pub fn register_lints(store: &mut rustc_lint::LintStore, conf: &'static Conf) {
store.register_late_pass(|_| Box::new(unneeded_struct_pattern::UnneededStructPattern));
store.register_late_pass(|_| Box::<unnecessary_semicolon::UnnecessarySemicolon>::default());
store.register_late_pass(move |_| Box::new(non_std_lazy_statics::NonStdLazyStatic::new(conf)));
store.register_late_pass(|_| Box::new(deref_coercions::DerefCoercions));
// add lints here, do not remove this comment, it's used in `new_lint`
}
7 changes: 7 additions & 0 deletions tests/ui/deref_coercions.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#![warn(clippy::deref_coercions)]
use std::ops::Deref;

fn main() {
let x = &Box::new(true);
let _: &bool = x;
}
12 changes: 12 additions & 0 deletions tests/ui/deref_coercions.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
error: implicit deref coercion
--> tests/ui/deref_coercions.rs:6:20
|
LL | let _: &bool = x;
| ^
|
= help: consider using `deref() or deref_mut()`
= note: `-D clippy::deref-coercions` implied by `-D warnings`
= help: to override `-D warnings` add `#[allow(clippy::deref_coercions)]`

error: aborting due to 1 previous error