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

should_implement_trait triggers on incompatible signatures #5612

Open
mahkoh opened this issue May 18, 2020 · 3 comments
Open

should_implement_trait triggers on incompatible signatures #5612

mahkoh opened this issue May 18, 2020 · 3 comments
Labels
C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have

Comments

@mahkoh
Copy link

mahkoh commented May 18, 2020

... and suggests implementing FromStr which has an incompatible signature.

@phansch phansch added C-bug Category: Clippy is not doing the correct thing I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied labels May 19, 2020
@ebroto
Copy link
Member

ebroto commented May 19, 2020

Is the signature considered incompatible because the return type differs? After a quick look (I may be mistaken) this seems to be the intended behavior (and should probably be allowed by the OP in this case).

Here, the return type is associated to OutType::Any...

("from_str", 1, &FN_HEADER, SelfKind::No, OutType::Any, "std::str::FromStr"),

... which means "not the unit type" according to this snippet ...

impl OutType {
fn matches(self, cx: &LateContext<'_, '_>, ty: &hir::FnRetTy<'_>) -> bool {
let is_unit = |ty: &hir::Ty<'_>| SpanlessEq::new(cx).eq_ty_kind(&ty.kind, &hir::TyKind::Tup(&[]));
match (self, ty) {
(Self::Unit, &hir::FnRetTy::DefaultReturn(_)) => true,
(Self::Unit, &hir::FnRetTy::Return(ref ty)) if is_unit(ty) => true,
(Self::Bool, &hir::FnRetTy::Return(ref ty)) if is_bool(ty) => true,
(Self::Any, &hir::FnRetTy::Return(ref ty)) if !is_unit(ty) => true,
(Self::Ref, &hir::FnRetTy::Return(ref ty)) => matches!(ty.kind, hir::TyKind::Rptr(_, _)),
_ => false,
}
}
}

@mahkoh
Copy link
Author

mahkoh commented May 19, 2020

The return type of FromStr is Self which is not compatible with &Self. In particular, Self might be unsized (just like str).

@camsteffen camsteffen added good-first-issue These issues are a good way to get started with Clippy I-false-positive Issue: The lint was triggered on code it shouldn't have and removed I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied labels Feb 18, 2021
@Alexendoo Alexendoo removed the good-first-issue These issues are a good way to get started with Clippy label Oct 15, 2022
@Alexendoo Alexendoo changed the title should_implement_trait triggers on fn from_str(&str) -> &Self should_implement_trait triggers on incompatible signatures Oct 15, 2022
@Alexendoo
Copy link
Member

Another example from #9638 is when the orphan rules prevent an impl, e.g. you can't impl FromIterator<String> for Option<LocalType> or impl FromStr for Option<LocalType>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have
Projects
None yet
Development

No branches or pull requests

5 participants