mirror of
https://github.com/rust-lang/rust.git
synced 2025-04-14 21:16:50 +00:00
use other
instead of self
This commit is contained in:
parent
004e89d4cf
commit
844afbfeba
@ -4834,6 +4834,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
|
||||
@ -5020,7 +5021,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
|
||||
|
@ -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,
|
||||
@ -469,7 +470,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,
|
||||
|
@ -1,10 +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::{def::Res, Expr, ExprKind, ImplItem, ImplItemKind, ItemKind, LangItem, Node, PatKind, UnOp};
|
||||
use rustc_hir::{ExprKind, ImplItem, ImplItemKind, Node, UnOp};
|
||||
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
|
||||
@ -45,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 {
|
||||
@ -68,10 +123,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(
|
||||
@ -116,5 +168,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,
|
||||
);
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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;
|
||||
|
@ -1,145 +0,0 @@
|
||||
use clippy_utils::{
|
||||
diagnostics::span_lint_and_then, get_parent_node, is_res_lang_ctor, path_res, ty::implements_trait,
|
||||
};
|
||||
use rustc_errors::Applicability;
|
||||
use rustc_hir::{def::Res, Expr, ExprKind, ImplItem, ImplItemKind, ItemKind, LangItem, Node, PatKind};
|
||||
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;
|
||||
use std::borrow::Cow;
|
||||
|
||||
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 NEEDLESS_PARTIAL_ORD_IMPL,
|
||||
correctness,
|
||||
"manual implementation of `PartialOrd` when `Ord` is already implemented"
|
||||
}
|
||||
declare_lint_pass!(NeedlessImpls => [NEEDLESS_PARTIAL_ORD_IMPL]);
|
||||
|
||||
impl LateLintPass<'_> for NeedlessImpls {
|
||||
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 {
|
||||
return;
|
||||
};
|
||||
let ItemKind::Impl(imp) = item.kind else {
|
||||
return;
|
||||
};
|
||||
let Some(trait_impl) = cx.tcx.impl_trait_ref(item.owner_id).map(EarlyBinder::skip_binder) else {
|
||||
return;
|
||||
};
|
||||
let trait_impl_def_id = trait_impl.def_id;
|
||||
if cx.tcx.is_automatically_derived(item.owner_id.to_def_id()) {
|
||||
return;
|
||||
}
|
||||
let ImplItemKind::Fn(_, impl_item_id) = cx.tcx.hir().impl_item(impl_item.impl_item_id()).kind else {
|
||||
return;
|
||||
};
|
||||
let body = cx.tcx.hir().body(impl_item_id);
|
||||
let ExprKind::Block(block, ..) = body.value.kind else {
|
||||
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 {
|
||||
span_lint_and_then(
|
||||
cx,
|
||||
NEEDLESS_PARTIAL_ORD_IMPL,
|
||||
item.span,
|
||||
"manual implementation of `PartialOrd` when `Ord` is already implemented",
|
||||
|diag| {
|
||||
let (help, app) = if let Some(other) = body.params.get(0)
|
||||
&& 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,
|
||||
);
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -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;
|
||||
|
@ -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;
|
||||
|
@ -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)]
|
||||
|
@ -1,5 +1,5 @@
|
||||
error: you are implementing `Clone` explicitly on a `Copy` type
|
||||
--> $DIR/derive.rs:7:1
|
||||
--> $DIR/derive.rs:11:1
|
||||
|
|
||||
LL | / impl Clone for Qux {
|
||||
LL | | fn clone(&self) -> Self {
|
||||
@ -9,7 +9,7 @@ LL | | }
|
||||
| |_^
|
||||
|
|
||||
note: consider deriving `Clone` or removing `Copy`
|
||||
--> $DIR/derive.rs:7:1
|
||||
--> $DIR/derive.rs:11:1
|
||||
|
|
||||
LL | / impl Clone for Qux {
|
||||
LL | | fn clone(&self) -> Self {
|
||||
@ -20,7 +20,7 @@ LL | | }
|
||||
= note: `-D clippy::expl-impl-clone-on-copy` implied by `-D warnings`
|
||||
|
||||
error: you are implementing `Clone` explicitly on a `Copy` type
|
||||
--> $DIR/derive.rs:31:1
|
||||
--> $DIR/derive.rs:35:1
|
||||
|
|
||||
LL | / impl<'a> Clone for Lt<'a> {
|
||||
LL | | fn clone(&self) -> Self {
|
||||
@ -30,7 +30,7 @@ LL | | }
|
||||
| |_^
|
||||
|
|
||||
note: consider deriving `Clone` or removing `Copy`
|
||||
--> $DIR/derive.rs:31:1
|
||||
--> $DIR/derive.rs:35:1
|
||||
|
|
||||
LL | / impl<'a> Clone for Lt<'a> {
|
||||
LL | | fn clone(&self) -> Self {
|
||||
@ -40,7 +40,7 @@ LL | | }
|
||||
| |_^
|
||||
|
||||
error: you are implementing `Clone` explicitly on a `Copy` type
|
||||
--> $DIR/derive.rs:42:1
|
||||
--> $DIR/derive.rs:46:1
|
||||
|
|
||||
LL | / impl Clone for BigArray {
|
||||
LL | | fn clone(&self) -> Self {
|
||||
@ -50,7 +50,7 @@ LL | | }
|
||||
| |_^
|
||||
|
|
||||
note: consider deriving `Clone` or removing `Copy`
|
||||
--> $DIR/derive.rs:42:1
|
||||
--> $DIR/derive.rs:46:1
|
||||
|
|
||||
LL | / impl Clone for BigArray {
|
||||
LL | | fn clone(&self) -> Self {
|
||||
@ -60,7 +60,7 @@ LL | | }
|
||||
| |_^
|
||||
|
||||
error: you are implementing `Clone` explicitly on a `Copy` type
|
||||
--> $DIR/derive.rs:53:1
|
||||
--> $DIR/derive.rs:57:1
|
||||
|
|
||||
LL | / impl Clone for FnPtr {
|
||||
LL | | fn clone(&self) -> Self {
|
||||
@ -70,7 +70,7 @@ LL | | }
|
||||
| |_^
|
||||
|
|
||||
note: consider deriving `Clone` or removing `Copy`
|
||||
--> $DIR/derive.rs:53:1
|
||||
--> $DIR/derive.rs:57:1
|
||||
|
|
||||
LL | / impl Clone for FnPtr {
|
||||
LL | | fn clone(&self) -> Self {
|
||||
@ -80,7 +80,7 @@ LL | | }
|
||||
| |_^
|
||||
|
||||
error: you are implementing `Clone` explicitly on a `Copy` type
|
||||
--> $DIR/derive.rs:73:1
|
||||
--> $DIR/derive.rs:77:1
|
||||
|
|
||||
LL | / impl<T: Clone> Clone for Generic2<T> {
|
||||
LL | | fn clone(&self) -> Self {
|
||||
@ -90,7 +90,7 @@ LL | | }
|
||||
| |_^
|
||||
|
|
||||
note: consider deriving `Clone` or removing `Copy`
|
||||
--> $DIR/derive.rs:73:1
|
||||
--> $DIR/derive.rs:77:1
|
||||
|
|
||||
LL | / impl<T: Clone> Clone for Generic2<T> {
|
||||
LL | | fn clone(&self) -> Self {
|
||||
|
@ -1,6 +1,6 @@
|
||||
#![warn(clippy::derive_ord_xor_partial_ord)]
|
||||
#![allow(clippy::unnecessary_wraps)]
|
||||
#![allow(clippy::needless_partial_ord_impl)]
|
||||
#![allow(clippy::incorrect_partial_ord_impl_on_ord_type)]
|
||||
|
||||
use std::cmp::Ordering;
|
||||
|
||||
|
@ -1,4 +1,3 @@
|
||||
//@run-rustfix
|
||||
#![allow(unused)]
|
||||
#![no_main]
|
||||
|
||||
@ -51,7 +50,7 @@ impl Ord for C {
|
||||
|
||||
impl PartialOrd for C {
|
||||
fn partial_cmp(&self, _: &Self) -> Option<Ordering> {
|
||||
todo!();
|
||||
todo!(); // don't run rustfix, or else this will cause it to fail to compile
|
||||
}
|
||||
}
|
||||
|
||||
@ -87,3 +86,32 @@ impl<A: Ord + PartialOrd> PartialOrd for Uwu<A> {
|
||||
todo!();
|
||||
}
|
||||
}
|
||||
|
||||
// do not lint since `Rhs` is not `Self`
|
||||
|
||||
#[derive(Eq, PartialEq)]
|
||||
struct F(u32);
|
||||
|
||||
impl Ord for F {
|
||||
fn cmp(&self, other: &Self) -> Ordering {
|
||||
todo!();
|
||||
}
|
||||
}
|
||||
|
||||
impl PartialOrd for F {
|
||||
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
|
||||
Some(self.cmp(other))
|
||||
}
|
||||
}
|
||||
|
||||
impl PartialEq<u32> for F {
|
||||
fn eq(&self, other: &u32) -> bool {
|
||||
todo!();
|
||||
}
|
||||
}
|
||||
|
||||
impl PartialOrd<u32> for F {
|
||||
fn partial_cmp(&self, other: &u32) -> Option<Ordering> {
|
||||
todo!();
|
||||
}
|
||||
}
|
28
tests/ui/incorrect_partial_ord_impl_on_ord_type.stderr
Normal file
28
tests/ui/incorrect_partial_ord_impl_on_ord_type.stderr
Normal file
@ -0,0 +1,28 @@
|
||||
error: incorrect implementation of `partial_cmp` on an `Ord` type
|
||||
--> $DIR/incorrect_partial_ord_impl_on_ord_type.rs:17:1
|
||||
|
|
||||
LL | / impl PartialOrd for A {
|
||||
LL | | fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
|
||||
| | _____________________________________________________________-
|
||||
LL | || todo!();
|
||||
LL | || }
|
||||
| ||_____- help: change this to: `{ Some(self.cmp(other)) }`
|
||||
LL | | }
|
||||
| |__^
|
||||
|
|
||||
= note: `#[deny(clippy::incorrect_partial_ord_impl_on_ord_type)]` on by default
|
||||
|
||||
error: incorrect implementation of `partial_cmp` on an `Ord` type
|
||||
--> $DIR/incorrect_partial_ord_impl_on_ord_type.rs:51:1
|
||||
|
|
||||
LL | / impl PartialOrd for C {
|
||||
LL | | fn partial_cmp(&self, _: &Self) -> Option<Ordering> {
|
||||
| | _________________________________________________________-
|
||||
LL | || todo!(); // don't run rustfix, or else this will cause it to fail to compile
|
||||
LL | || }
|
||||
| ||_____- help: change this to: `{ Some(self.cmp(...)) }`
|
||||
LL | | }
|
||||
| |__^
|
||||
|
||||
error: aborting due to 2 previous errors
|
||||
|
@ -1,85 +0,0 @@
|
||||
//@run-rustfix
|
||||
#![allow(unused)]
|
||||
#![no_main]
|
||||
|
||||
use std::cmp::Ordering;
|
||||
|
||||
// lint
|
||||
|
||||
#[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(self)) }
|
||||
}
|
||||
|
||||
// do not lint
|
||||
|
||||
#[derive(Eq, PartialEq)]
|
||||
struct B(u32);
|
||||
|
||||
impl Ord for B {
|
||||
fn cmp(&self, other: &Self) -> Ordering {
|
||||
todo!();
|
||||
}
|
||||
}
|
||||
|
||||
impl PartialOrd for B {
|
||||
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
|
||||
Some(self.cmp(other))
|
||||
}
|
||||
}
|
||||
|
||||
// lint, but we can't give a suggestion since &Self is not named
|
||||
|
||||
#[derive(Eq, PartialEq)]
|
||||
struct C(u32);
|
||||
|
||||
impl Ord for C {
|
||||
fn cmp(&self, other: &Self) -> Ordering {
|
||||
todo!();
|
||||
}
|
||||
}
|
||||
|
||||
impl PartialOrd for C {
|
||||
fn partial_cmp(&self, _: &Self) -> Option<Ordering> { Some(self.cmp(self)) }
|
||||
}
|
||||
|
||||
// do not lint derived
|
||||
|
||||
#[derive(Eq, Ord, PartialEq, PartialOrd)]
|
||||
struct D(u32);
|
||||
|
||||
// do not lint if ord is not manually implemented
|
||||
|
||||
#[derive(Eq, PartialEq)]
|
||||
struct E(u32);
|
||||
|
||||
impl PartialOrd for E {
|
||||
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
|
||||
todo!();
|
||||
}
|
||||
}
|
||||
|
||||
// do not lint since ord has more restrictive bounds
|
||||
|
||||
#[derive(Eq, PartialEq)]
|
||||
struct Uwu<A>(A);
|
||||
|
||||
impl<A: std::fmt::Debug + Ord + PartialOrd> Ord for Uwu<A> {
|
||||
fn cmp(&self, other: &Self) -> Ordering {
|
||||
todo!();
|
||||
}
|
||||
}
|
||||
|
||||
impl<A: Ord + PartialOrd> PartialOrd for Uwu<A> {
|
||||
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
|
||||
todo!();
|
||||
}
|
||||
}
|
@ -1,28 +0,0 @@
|
||||
error: manual implementation of `PartialOrd` when `Ord` is already implemented
|
||||
--> $DIR/needless_partial_ord_impl.rs:18:1
|
||||
|
|
||||
LL | / impl PartialOrd for A {
|
||||
LL | | fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
|
||||
| | _____________________________________________________________-
|
||||
LL | || todo!();
|
||||
LL | || }
|
||||
| ||_____- help: change this to: `{ Some(self.cmp(self)) }`
|
||||
LL | | }
|
||||
| |__^
|
||||
|
|
||||
= note: `#[deny(clippy::needless_partial_ord_impl)]` on by default
|
||||
|
||||
error: manual implementation of `PartialOrd` when `Ord` is already implemented
|
||||
--> $DIR/needless_partial_ord_impl.rs:52:1
|
||||
|
|
||||
LL | / impl PartialOrd for C {
|
||||
LL | | fn partial_cmp(&self, _: &Self) -> Option<Ordering> {
|
||||
| | _________________________________________________________-
|
||||
LL | || todo!();
|
||||
LL | || }
|
||||
| ||_____- help: change this to: `{ Some(self.cmp(self)) }`
|
||||
LL | | }
|
||||
| |__^
|
||||
|
||||
error: aborting due to 2 previous errors
|
||||
|
Loading…
Reference in New Issue
Block a user