Skip to content

Commit

Permalink
use other instead of self
Browse files Browse the repository at this point in the history
  • Loading branch information
Centri3 committed Jun 30, 2023
1 parent 9ff890d commit 89c37e3
Show file tree
Hide file tree
Showing 14 changed files with 202 additions and 284 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4861,6 +4861,7 @@ Released 2018-09-13
[`inconsistent_digit_grouping`]: https://rust-lang.github.io/rust-clippy/master/index.html#inconsistent_digit_grouping
[`inconsistent_struct_constructor`]: https://rust-lang.github.io/rust-clippy/master/index.html#inconsistent_struct_constructor
[`incorrect_clone_impl_on_copy_type`]: https://rust-lang.github.io/rust-clippy/master/index.html#incorrect_clone_impl_on_copy_type
[`incorrect_partial_ord_impl_on_ord_type`]: https://rust-lang.github.io/rust-clippy/master/index.html#incorrect_partial_ord_impl_on_ord_type
[`index_refutable_slice`]: https://rust-lang.github.io/rust-clippy/master/index.html#index_refutable_slice
[`indexing_slicing`]: https://rust-lang.github.io/rust-clippy/master/index.html#indexing_slicing
[`ineffective_bit_mask`]: https://rust-lang.github.io/rust-clippy/master/index.html#ineffective_bit_mask
Expand Down Expand Up @@ -5046,7 +5047,6 @@ Released 2018-09-13
[`needless_option_as_deref`]: https://rust-lang.github.io/rust-clippy/master/index.html#needless_option_as_deref
[`needless_option_take`]: https://rust-lang.github.io/rust-clippy/master/index.html#needless_option_take
[`needless_parens_on_range_literals`]: https://rust-lang.github.io/rust-clippy/master/index.html#needless_parens_on_range_literals
[`needless_partial_ord_impl`]: https://rust-lang.github.io/rust-clippy/master/index.html#needless_partial_ord_impl
[`needless_pass_by_value`]: https://rust-lang.github.io/rust-clippy/master/index.html#needless_pass_by_value
[`needless_pub_self`]: https://rust-lang.github.io/rust-clippy/master/index.html#needless_pub_self
[`needless_question_mark`]: https://rust-lang.github.io/rust-clippy/master/index.html#needless_question_mark
Expand Down
2 changes: 1 addition & 1 deletion clippy_lints/src/declared_lints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,7 @@ pub(crate) static LINTS: &[&crate::LintInfo] = &[
crate::implicit_saturating_sub::IMPLICIT_SATURATING_SUB_INFO,
crate::inconsistent_struct_constructor::INCONSISTENT_STRUCT_CONSTRUCTOR_INFO,
crate::incorrect_impls::INCORRECT_CLONE_IMPL_ON_COPY_TYPE_INFO,
crate::incorrect_impls::INCORRECT_PARTIAL_ORD_IMPL_ON_ORD_TYPE_INFO,
crate::index_refutable_slice::INDEX_REFUTABLE_SLICE_INFO,
crate::indexing_slicing::INDEXING_SLICING_INFO,
crate::indexing_slicing::OUT_OF_BOUNDS_INDEXING_INFO,
Expand Down Expand Up @@ -466,7 +467,6 @@ pub(crate) static LINTS: &[&crate::LintInfo] = &[
crate::needless_else::NEEDLESS_ELSE_INFO,
crate::needless_for_each::NEEDLESS_FOR_EACH_INFO,
crate::needless_if::NEEDLESS_IF_INFO,
crate::needless_impls::NEEDLESS_PARTIAL_ORD_IMPL_INFO,
crate::needless_late_init::NEEDLESS_LATE_INIT_INFO,
crate::needless_parens_on_range_literals::NEEDLESS_PARENS_ON_RANGE_LITERALS_INFO,
crate::needless_pass_by_value::NEEDLESS_PASS_BY_VALUE_INFO,
Expand Down
131 changes: 124 additions & 7 deletions clippy_lints/src/incorrect_impls.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
use clippy_utils::{diagnostics::span_lint_and_sugg, get_parent_node, last_path_segment, ty::implements_trait};
use clippy_utils::{
diagnostics::{span_lint_and_sugg, span_lint_and_then},
get_parent_node, is_res_lang_ctor, last_path_segment, path_res,
ty::implements_trait,
};
use rustc_errors::Applicability;
use rustc_hir::{ExprKind, ImplItem, ImplItemKind, ItemKind, Node, UnOp};
use rustc_hir::{def::Res, Expr, ExprKind, ImplItem, ImplItemKind, ItemKind, LangItem, Node, PatKind, UnOp};
use rustc_hir_analysis::hir_ty_to_ty;
use rustc_lint::{LateContext, LateLintPass};
use rustc_middle::ty::EarlyBinder;
use rustc_session::{declare_lint_pass, declare_tool_lint};
use rustc_span::{sym, symbol};
use std::borrow::Cow;

declare_clippy_lint! {
/// ### What it does
Expand Down Expand Up @@ -46,10 +51,59 @@ declare_clippy_lint! {
correctness,
"manual implementation of `Clone` on a `Copy` type"
}
declare_lint_pass!(IncorrectImpls => [INCORRECT_CLONE_IMPL_ON_COPY_TYPE]);
declare_clippy_lint! {
/// ### What it does
/// Checks for manual implementations of both `PartialOrd` and `Ord` when only `Ord` is
/// necessary.
///
/// ### Why is this bad?
/// If both `PartialOrd` and `Ord` are implemented, they must agree. This is commonly done by
/// wrapping the result of `cmp` in `Some` for `partial_cmp`. Not doing this may silently
/// introduce an error upon refactoring.
///
/// ### Example
/// ```rust,ignore
/// #[derive(Eq, PartialEq)]
/// struct A(u32);
///
/// impl Ord for A {
/// fn cmp(&self, other: &Self) -> Ordering {
/// todo!();
/// }
/// }
///
/// impl PartialOrd for A {
/// fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
/// todo!();
/// }
/// }
/// ```
/// Use instead:
/// ```rust,ignore
/// #[derive(Eq, PartialEq)]
/// struct A(u32);
///
/// impl Ord for A {
/// fn cmp(&self, other: &Self) -> Ordering {
/// todo!();
/// }
/// }
///
/// impl PartialOrd for A {
/// fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
/// Some(self.cmp(other))
/// }
/// }
/// ```
#[clippy::version = "1.72.0"]
pub INCORRECT_PARTIAL_ORD_IMPL_ON_ORD_TYPE,
correctness,
"manual implementation of `PartialOrd` when `Ord` is already implemented"
}
declare_lint_pass!(IncorrectImpls => [INCORRECT_CLONE_IMPL_ON_COPY_TYPE, INCORRECT_PARTIAL_ORD_IMPL_ON_ORD_TYPE]);

impl LateLintPass<'_> for IncorrectImpls {
#[expect(clippy::needless_return)]
#[expect(clippy::too_many_lines)]
fn check_impl_item(&mut self, cx: &LateContext<'_>, impl_item: &ImplItem<'_>) {
let node = get_parent_node(cx.tcx, impl_item.hir_id());
let Some(Node::Item(item)) = node else {
Expand All @@ -72,10 +126,7 @@ impl LateLintPass<'_> for IncorrectImpls {
let ExprKind::Block(block, ..) = body.value.kind else {
return;
};
// Above is duplicated from the `duplicate_manual_partial_ord_impl` branch.
// Remove it while solving conflicts once that PR is merged.

// Actual implementation; remove this comment once aforementioned PR is merged
if cx.tcx.is_diagnostic_item(sym::Clone, trait_impl_def_id)
&& let Some(copy_def_id) = cx.tcx.get_diagnostic_item(sym::Copy)
&& implements_trait(
Expand Down Expand Up @@ -120,5 +171,71 @@ impl LateLintPass<'_> for IncorrectImpls {
return;
}
}

if cx.tcx.is_diagnostic_item(sym::PartialOrd, trait_impl_def_id)
&& impl_item.ident.name == sym::partial_cmp
&& let Some(ord_def_id) = cx
.tcx
.diagnostic_items(trait_impl.def_id.krate)
.name_to_id
.get(&sym::Ord)
&& implements_trait(
cx,
hir_ty_to_ty(cx.tcx, imp.self_ty),
*ord_def_id,
trait_impl.substs,
)
{
if block.stmts.is_empty()
&& let Some(expr) = block.expr
&& let ExprKind::Call(
Expr {
kind: ExprKind::Path(some_path),
hir_id: some_hir_id,
..
},
[cmp_expr],
) = expr.kind
&& is_res_lang_ctor(cx, cx.qpath_res(some_path, *some_hir_id), LangItem::OptionSome)
&& let ExprKind::MethodCall(cmp_path, _, [other_expr], ..) = cmp_expr.kind
&& cmp_path.ident.name == sym::cmp
&& let Res::Local(..) = path_res(cx, other_expr)
{} else {
// If lhs and rhs are not the same type, bail. This makes creating a valid
// suggestion tons more complex.
if let Some(lhs) = trait_impl.substs.get(0)
&& let Some(rhs) = trait_impl.substs.get(1)
&& lhs != rhs
{
return;
}

span_lint_and_then(
cx,
INCORRECT_PARTIAL_ORD_IMPL_ON_ORD_TYPE,
item.span,
"incorrect implementation of `partial_cmp` on an `Ord` type",
|diag| {
let (help, app) = if let Some(other) = body.params.get(1)
&& let PatKind::Binding(_, _, other_ident, ..) = other.pat.kind
{
(
Cow::Owned(format!("{{ Some(self.cmp({})) }}", other_ident.name)),
Applicability::Unspecified,
)
} else {
(Cow::Borrowed("{ Some(self.cmp(...)) }"), Applicability::HasPlaceholders)
};

diag.span_suggestion(
block.span,
"change this to",
help,
app,
);
}
);
}
}
}
}
1 change: 0 additions & 1 deletion clippy_lints/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,6 @@ mod needless_continue;
mod needless_else;
mod needless_for_each;
mod needless_if;
mod needless_impls;
mod needless_late_init;
mod needless_parens_on_range_literals;
mod needless_pass_by_value;
Expand Down
145 changes: 0 additions & 145 deletions clippy_lints/src/needless_impls.rs

This file was deleted.

2 changes: 1 addition & 1 deletion tests/ui/bool_comparison.fixed
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

#![allow(clippy::needless_if)]
#![warn(clippy::bool_comparison)]
#![allow(clippy::needless_partial_ord_impl)]
#![allow(clippy::incorrect_partial_ord_impl_on_ord_type)]

fn main() {
let x = true;
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/bool_comparison.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

#![allow(clippy::needless_if)]
#![warn(clippy::bool_comparison)]
#![allow(clippy::needless_partial_ord_impl)]
#![allow(clippy::incorrect_partial_ord_impl_on_ord_type)]

fn main() {
let x = true;
Expand Down
6 changes: 5 additions & 1 deletion tests/ui/derive.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
#![allow(clippy::incorrect_clone_impl_on_copy_type, dead_code)]
#![allow(
clippy::incorrect_clone_impl_on_copy_type,
clippy::incorrect_partial_ord_impl_on_ord_type,
dead_code
)]
#![warn(clippy::expl_impl_clone_on_copy)]

#[derive(Copy)]
Expand Down
Loading

0 comments on commit 89c37e3

Please sign in to comment.