-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Include async functions in the len_without_is_empty #10359
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
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 hidden or 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 | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
@@ -6,8 +6,9 @@ use rustc_ast::ast::LitKind; | |||||||||
use rustc_errors::Applicability; | ||||||||||
use rustc_hir::def_id::DefIdSet; | ||||||||||
use rustc_hir::{ | ||||||||||
def_id::DefId, AssocItemKind, BinOpKind, Expr, ExprKind, FnRetTy, ImplItem, ImplItemKind, ImplicitSelfKind, Item, | ||||||||||
ItemKind, Mutability, Node, TraitItemRef, TyKind, | ||||||||||
def::Res, def_id::DefId, lang_items::LangItem, AssocItemKind, BinOpKind, Expr, ExprKind, FnRetTy, GenericArg, | ||||||||||
GenericBound, ImplItem, ImplItemKind, ImplicitSelfKind, Item, ItemKind, Mutability, Node, PathSegment, PrimTy, | ||||||||||
QPath, TraitItemRef, TyKind, TypeBindingKind, | ||||||||||
}; | ||||||||||
use rustc_lint::{LateContext, LateLintPass}; | ||||||||||
use rustc_middle::ty::{self, AssocKind, FnSig, Ty}; | ||||||||||
|
@@ -250,33 +251,98 @@ fn check_trait_items(cx: &LateContext<'_>, visited_trait: &Item<'_>, trait_items | |||||||||
} | ||||||||||
|
||||||||||
#[derive(Debug, Clone, Copy)] | ||||||||||
enum LenOutput<'tcx> { | ||||||||||
enum LenOutput { | ||||||||||
Integral, | ||||||||||
Option(DefId), | ||||||||||
Result(DefId, Ty<'tcx>), | ||||||||||
Result(DefId), | ||||||||||
} | ||||||||||
fn parse_len_output<'tcx>(cx: &LateContext<'_>, sig: FnSig<'tcx>) -> Option<LenOutput<'tcx>> { | ||||||||||
|
||||||||||
fn extract_future_output<'tcx>(cx: &LateContext<'tcx>, ty: Ty<'tcx>) -> Option<&'tcx PathSegment<'tcx>> { | ||||||||||
if let ty::Alias(_, alias_ty) = ty.kind() && | ||||||||||
let Some(Node::Item(item)) = cx.tcx.hir().get_if_local(alias_ty.def_id) && | ||||||||||
let Item { kind: ItemKind::OpaqueTy(opaque), .. } = item && | ||||||||||
opaque.bounds.len() == 1 && | ||||||||||
let GenericBound::LangItemTrait(LangItem::Future, _, _, generic_args) = &opaque.bounds[0] && | ||||||||||
generic_args.bindings.len() == 1 && | ||||||||||
let TypeBindingKind::Equality { | ||||||||||
term: rustc_hir::Term::Ty(rustc_hir::Ty {kind: TyKind::Path(QPath::Resolved(_, path)), .. }), | ||||||||||
} = &generic_args.bindings[0].kind && | ||||||||||
path.segments.len() == 1 { | ||||||||||
return Some(&path.segments[0]); | ||||||||||
} | ||||||||||
|
||||||||||
None | ||||||||||
} | ||||||||||
|
||||||||||
fn is_first_generic_integral<'tcx>(segment: &'tcx PathSegment<'tcx>) -> bool { | ||||||||||
if let Some(generic_args) = segment.args { | ||||||||||
if generic_args.args.is_empty() { | ||||||||||
return false; | ||||||||||
} | ||||||||||
let arg = &generic_args.args[0]; | ||||||||||
if let GenericArg::Type(rustc_hir::Ty { | ||||||||||
kind: TyKind::Path(QPath::Resolved(_, path)), | ||||||||||
.. | ||||||||||
}) = arg | ||||||||||
{ | ||||||||||
let segments = &path.segments; | ||||||||||
let segment = &segments[0]; | ||||||||||
let res = &segment.res; | ||||||||||
if matches!(res, Res::PrimTy(PrimTy::Uint(_))) || matches!(res, Res::PrimTy(PrimTy::Int(_))) { | ||||||||||
return true; | ||||||||||
} | ||||||||||
} | ||||||||||
} | ||||||||||
|
||||||||||
false | ||||||||||
} | ||||||||||
|
||||||||||
fn parse_len_output<'tcx>(cx: &LateContext<'tcx>, sig: FnSig<'tcx>) -> Option<LenOutput> { | ||||||||||
if let Some(segment) = extract_future_output(cx, sig.output()) { | ||||||||||
let res = segment.res; | ||||||||||
|
||||||||||
if matches!(res, Res::PrimTy(PrimTy::Uint(_))) || matches!(res, Res::PrimTy(PrimTy::Int(_))) { | ||||||||||
return Some(LenOutput::Integral); | ||||||||||
} | ||||||||||
|
||||||||||
if let Res::Def(_, def_id) = res { | ||||||||||
if cx.tcx.is_diagnostic_item(sym::Option, def_id) && is_first_generic_integral(segment) { | ||||||||||
return Some(LenOutput::Option(def_id)); | ||||||||||
} else if cx.tcx.is_diagnostic_item(sym::Result, def_id) && is_first_generic_integral(segment) { | ||||||||||
return Some(LenOutput::Result(def_id)); | ||||||||||
} | ||||||||||
} | ||||||||||
|
||||||||||
return None; | ||||||||||
} | ||||||||||
|
||||||||||
match *sig.output().kind() { | ||||||||||
ty::Int(_) | ty::Uint(_) => Some(LenOutput::Integral), | ||||||||||
ty::Adt(adt, subs) if cx.tcx.is_diagnostic_item(sym::Option, adt.did()) => { | ||||||||||
subs.type_at(0).is_integral().then(|| LenOutput::Option(adt.did())) | ||||||||||
}, | ||||||||||
ty::Adt(adt, subs) if cx.tcx.is_diagnostic_item(sym::Result, adt.did()) => subs | ||||||||||
.type_at(0) | ||||||||||
.is_integral() | ||||||||||
.then(|| LenOutput::Result(adt.did(), subs.type_at(1))), | ||||||||||
ty::Adt(adt, subs) if cx.tcx.is_diagnostic_item(sym::Result, adt.did()) => { | ||||||||||
subs.type_at(0).is_integral().then(|| LenOutput::Result(adt.did())) | ||||||||||
}, | ||||||||||
_ => None, | ||||||||||
} | ||||||||||
} | ||||||||||
|
||||||||||
impl<'tcx> LenOutput<'tcx> { | ||||||||||
fn matches_is_empty_output(self, ty: Ty<'tcx>) -> bool { | ||||||||||
impl LenOutput { | ||||||||||
fn matches_is_empty_output<'tcx>(self, cx: &LateContext<'tcx>, ty: Ty<'tcx>) -> bool { | ||||||||||
if let Some(segment) = extract_future_output(cx, ty) { | ||||||||||
return match (self, segment.res) { | ||||||||||
(_, Res::PrimTy(PrimTy::Bool)) => true, | ||||||||||
(Self::Option(_), Res::Def(_, def_id)) if cx.tcx.is_diagnostic_item(sym::Option, def_id) => true, | ||||||||||
(Self::Result(_), Res::Def(_, def_id)) if cx.tcx.is_diagnostic_item(sym::Result, def_id) => true, | ||||||||||
Comment on lines
+336
to
+337
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
I think we don't need guards here. |
||||||||||
_ => false, | ||||||||||
}; | ||||||||||
} | ||||||||||
|
||||||||||
match (self, ty.kind()) { | ||||||||||
(_, &ty::Bool) => true, | ||||||||||
(Self::Option(id), &ty::Adt(adt, subs)) if id == adt.did() => subs.type_at(0).is_bool(), | ||||||||||
(Self::Result(id, err_ty), &ty::Adt(adt, subs)) if id == adt.did() => { | ||||||||||
subs.type_at(0).is_bool() && subs.type_at(1) == err_ty | ||||||||||
}, | ||||||||||
(Self::Result(id), &ty::Adt(adt, subs)) if id == adt.did() => subs.type_at(0).is_bool(), | ||||||||||
_ => false, | ||||||||||
} | ||||||||||
} | ||||||||||
|
@@ -300,9 +366,14 @@ impl<'tcx> LenOutput<'tcx> { | |||||||||
} | ||||||||||
|
||||||||||
/// Checks if the given signature matches the expectations for `is_empty` | ||||||||||
fn check_is_empty_sig<'tcx>(sig: FnSig<'tcx>, self_kind: ImplicitSelfKind, len_output: LenOutput<'tcx>) -> bool { | ||||||||||
fn check_is_empty_sig<'tcx>( | ||||||||||
cx: &LateContext<'tcx>, | ||||||||||
sig: FnSig<'tcx>, | ||||||||||
self_kind: ImplicitSelfKind, | ||||||||||
len_output: LenOutput, | ||||||||||
) -> bool { | ||||||||||
match &**sig.inputs_and_output { | ||||||||||
[arg, res] if len_output.matches_is_empty_output(*res) => { | ||||||||||
[arg, res] if len_output.matches_is_empty_output(cx, *res) => { | ||||||||||
matches!( | ||||||||||
(arg.kind(), self_kind), | ||||||||||
(ty::Ref(_, _, Mutability::Not), ImplicitSelfKind::ImmRef) | ||||||||||
|
@@ -314,11 +385,11 @@ fn check_is_empty_sig<'tcx>(sig: FnSig<'tcx>, self_kind: ImplicitSelfKind, len_o | |||||||||
} | ||||||||||
|
||||||||||
/// Checks if the given type has an `is_empty` method with the appropriate signature. | ||||||||||
fn check_for_is_empty<'tcx>( | ||||||||||
cx: &LateContext<'tcx>, | ||||||||||
fn check_for_is_empty( | ||||||||||
cx: &LateContext<'_>, | ||||||||||
span: Span, | ||||||||||
self_kind: ImplicitSelfKind, | ||||||||||
output: LenOutput<'tcx>, | ||||||||||
output: LenOutput, | ||||||||||
impl_ty: DefId, | ||||||||||
item_name: Symbol, | ||||||||||
item_kind: &str, | ||||||||||
|
@@ -351,6 +422,7 @@ fn check_for_is_empty<'tcx>( | |||||||||
Some(is_empty) | ||||||||||
if !(is_empty.fn_has_self_parameter | ||||||||||
&& check_is_empty_sig( | ||||||||||
cx, | ||||||||||
cx.tcx.fn_sig(is_empty.def_id).subst_identity().skip_binder(), | ||||||||||
self_kind, | ||||||||||
output, | ||||||||||
|
This file contains hidden or 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 hidden or 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
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.
What is the use of the
if is_first_generic_integral(segment)
above if it returns the same thing anyway?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, this is a bug.
is_first_generic_integral
checks thatlen()
returnsOption<T>
where T is an integral type. This should ignore things likepub fn len() -> Option<String>
because that's what happens for the sync version.I've added a test for this.