Skip to content

Commit 1335978

Browse files
mladedavoppiliappan
andcommitted
Include async functions in the len_without_is_empty
Co-authored-by: Akshay <[email protected]>
1 parent 52c8b53 commit 1335978

File tree

3 files changed

+202
-20
lines changed

3 files changed

+202
-20
lines changed

clippy_lints/src/len_zero.rs

+91-19
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@ use rustc_ast::ast::LitKind;
66
use rustc_errors::Applicability;
77
use rustc_hir::def_id::DefIdSet;
88
use rustc_hir::{
9-
def_id::DefId, AssocItemKind, BinOpKind, Expr, ExprKind, FnRetTy, ImplItem, ImplItemKind, ImplicitSelfKind, Item,
10-
ItemKind, Mutability, Node, TraitItemRef, TyKind, UnOp,
9+
def::Res, def_id::DefId, lang_items::LangItem, AssocItemKind, BinOpKind, Expr, ExprKind, FnRetTy, GenericArg,
10+
GenericBound, ImplItem, ImplItemKind, ImplicitSelfKind, Item, ItemKind, Mutability, Node, PathSegment, PrimTy,
11+
QPath, TraitItemRef, TyKind, TypeBindingKind, UnOp,
1112
};
1213
use rustc_lint::{LateContext, LateLintPass};
1314
use rustc_middle::ty::{self, AssocKind, FnSig, Ty};
@@ -251,33 +252,98 @@ fn check_trait_items(cx: &LateContext<'_>, visited_trait: &Item<'_>, trait_items
251252
}
252253

253254
#[derive(Debug, Clone, Copy)]
254-
enum LenOutput<'tcx> {
255+
enum LenOutput {
255256
Integral,
256257
Option(DefId),
257-
Result(DefId, Ty<'tcx>),
258+
Result(DefId),
258259
}
259-
fn parse_len_output<'tcx>(cx: &LateContext<'_>, sig: FnSig<'tcx>) -> Option<LenOutput<'tcx>> {
260+
261+
fn extract_future_output<'tcx>(cx: &LateContext<'tcx>, ty: Ty<'tcx>) -> Option<&'tcx PathSegment<'tcx>> {
262+
if let ty::Alias(_, alias_ty) = ty.kind() &&
263+
let Some(Node::Item(item)) = cx.tcx.hir().get_if_local(alias_ty.def_id) &&
264+
let Item { kind: ItemKind::OpaqueTy(opaque), .. } = item &&
265+
opaque.bounds.len() == 1 &&
266+
let GenericBound::LangItemTrait(LangItem::Future, _, _, generic_args) = &opaque.bounds[0] &&
267+
generic_args.bindings.len() == 1 &&
268+
let TypeBindingKind::Equality {
269+
term: rustc_hir::Term::Ty(rustc_hir::Ty {kind: TyKind::Path(QPath::Resolved(_, path)), .. }),
270+
} = &generic_args.bindings[0].kind &&
271+
path.segments.len() == 1 {
272+
return Some(&path.segments[0]);
273+
}
274+
275+
None
276+
}
277+
278+
fn is_first_generic_integral<'tcx>(segment: &'tcx PathSegment<'tcx>) -> bool {
279+
if let Some(generic_args) = segment.args {
280+
if generic_args.args.is_empty() {
281+
return false;
282+
}
283+
let arg = &generic_args.args[0];
284+
if let GenericArg::Type(rustc_hir::Ty {
285+
kind: TyKind::Path(QPath::Resolved(_, path)),
286+
..
287+
}) = arg
288+
{
289+
let segments = &path.segments;
290+
let segment = &segments[0];
291+
let res = &segment.res;
292+
if matches!(res, Res::PrimTy(PrimTy::Uint(_))) || matches!(res, Res::PrimTy(PrimTy::Int(_))) {
293+
return true;
294+
}
295+
}
296+
}
297+
298+
false
299+
}
300+
301+
fn parse_len_output<'tcx>(cx: &LateContext<'tcx>, sig: FnSig<'tcx>) -> Option<LenOutput> {
302+
if let Some(segment) = extract_future_output(cx, sig.output()) {
303+
let res = segment.res;
304+
305+
if matches!(res, Res::PrimTy(PrimTy::Uint(_))) || matches!(res, Res::PrimTy(PrimTy::Int(_))) {
306+
return Some(LenOutput::Integral);
307+
}
308+
309+
if let Res::Def(_, def_id) = res {
310+
if cx.tcx.is_diagnostic_item(sym::Option, def_id) && is_first_generic_integral(segment) {
311+
return Some(LenOutput::Option(def_id));
312+
} else if cx.tcx.is_diagnostic_item(sym::Result, def_id) && is_first_generic_integral(segment) {
313+
return Some(LenOutput::Result(def_id));
314+
}
315+
}
316+
317+
return None;
318+
}
319+
260320
match *sig.output().kind() {
261321
ty::Int(_) | ty::Uint(_) => Some(LenOutput::Integral),
262322
ty::Adt(adt, subs) if cx.tcx.is_diagnostic_item(sym::Option, adt.did()) => {
263323
subs.type_at(0).is_integral().then(|| LenOutput::Option(adt.did()))
264324
},
265-
ty::Adt(adt, subs) if cx.tcx.is_diagnostic_item(sym::Result, adt.did()) => subs
266-
.type_at(0)
267-
.is_integral()
268-
.then(|| LenOutput::Result(adt.did(), subs.type_at(1))),
325+
ty::Adt(adt, subs) if cx.tcx.is_diagnostic_item(sym::Result, adt.did()) => {
326+
subs.type_at(0).is_integral().then(|| LenOutput::Result(adt.did()))
327+
},
269328
_ => None,
270329
}
271330
}
272331

273-
impl<'tcx> LenOutput<'tcx> {
274-
fn matches_is_empty_output(self, ty: Ty<'tcx>) -> bool {
332+
impl LenOutput {
333+
fn matches_is_empty_output<'tcx>(self, cx: &LateContext<'tcx>, ty: Ty<'tcx>) -> bool {
334+
if let Some(segment) = extract_future_output(cx, ty) {
335+
return match (self, segment.res) {
336+
(_, Res::PrimTy(PrimTy::Bool)) => true,
337+
(Self::Option(_), Res::Def(_, def_id)) if cx.tcx.is_diagnostic_item(sym::Option, def_id) => true,
338+
(Self::Result(_), Res::Def(_, def_id)) if cx.tcx.is_diagnostic_item(sym::Result, def_id) => true,
339+
_ => false,
340+
};
341+
}
342+
275343
match (self, ty.kind()) {
276344
(_, &ty::Bool) => true,
277345
(Self::Option(id), &ty::Adt(adt, subs)) if id == adt.did() => subs.type_at(0).is_bool(),
278-
(Self::Result(id, err_ty), &ty::Adt(adt, subs)) if id == adt.did() => {
279-
subs.type_at(0).is_bool() && subs.type_at(1) == err_ty
280-
},
346+
(Self::Result(id), &ty::Adt(adt, subs)) if id == adt.did() => subs.type_at(0).is_bool(),
281347
_ => false,
282348
}
283349
}
@@ -301,9 +367,14 @@ impl<'tcx> LenOutput<'tcx> {
301367
}
302368

303369
/// Checks if the given signature matches the expectations for `is_empty`
304-
fn check_is_empty_sig<'tcx>(sig: FnSig<'tcx>, self_kind: ImplicitSelfKind, len_output: LenOutput<'tcx>) -> bool {
370+
fn check_is_empty_sig<'tcx>(
371+
cx: &LateContext<'tcx>,
372+
sig: FnSig<'tcx>,
373+
self_kind: ImplicitSelfKind,
374+
len_output: LenOutput,
375+
) -> bool {
305376
match &**sig.inputs_and_output {
306-
[arg, res] if len_output.matches_is_empty_output(*res) => {
377+
[arg, res] if len_output.matches_is_empty_output(cx, *res) => {
307378
matches!(
308379
(arg.kind(), self_kind),
309380
(ty::Ref(_, _, Mutability::Not), ImplicitSelfKind::ImmRef)
@@ -315,11 +386,11 @@ fn check_is_empty_sig<'tcx>(sig: FnSig<'tcx>, self_kind: ImplicitSelfKind, len_o
315386
}
316387

317388
/// Checks if the given type has an `is_empty` method with the appropriate signature.
318-
fn check_for_is_empty<'tcx>(
319-
cx: &LateContext<'tcx>,
389+
fn check_for_is_empty(
390+
cx: &LateContext<'_>,
320391
span: Span,
321392
self_kind: ImplicitSelfKind,
322-
output: LenOutput<'tcx>,
393+
output: LenOutput,
323394
impl_ty: DefId,
324395
item_name: Symbol,
325396
item_kind: &str,
@@ -352,6 +423,7 @@ fn check_for_is_empty<'tcx>(
352423
Some(is_empty)
353424
if !(is_empty.fn_has_self_parameter
354425
&& check_is_empty_sig(
426+
cx,
355427
cx.tcx.fn_sig(is_empty.def_id).subst_identity().skip_binder(),
356428
self_kind,
357429
output,

tests/ui/len_without_is_empty.rs

+92
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,87 @@ impl AsyncLen {
282282
}
283283
}
284284

285+
// issue #7232
286+
pub struct AsyncLenWithoutIsEmpty;
287+
impl AsyncLenWithoutIsEmpty {
288+
pub async fn async_task(&self) -> bool {
289+
true
290+
}
291+
292+
pub async fn len(&self) -> usize {
293+
usize::from(!self.async_task().await)
294+
}
295+
}
296+
297+
// issue #7232
298+
pub struct AsyncOptionLenWithoutIsEmpty;
299+
impl AsyncOptionLenWithoutIsEmpty {
300+
async fn async_task(&self) -> bool {
301+
true
302+
}
303+
304+
pub async fn len(&self) -> Option<usize> {
305+
None
306+
}
307+
}
308+
309+
// issue #7232
310+
pub struct AsyncOptionLenNonIntegral;
311+
impl AsyncOptionLenNonIntegral {
312+
// don't lint
313+
pub async fn len(&self) -> Option<String> {
314+
None
315+
}
316+
}
317+
318+
// issue #7232
319+
pub struct AsyncResultLenWithoutIsEmpty;
320+
impl AsyncResultLenWithoutIsEmpty {
321+
async fn async_task(&self) -> bool {
322+
true
323+
}
324+
325+
pub async fn len(&self) -> Result<usize, ()> {
326+
Err(())
327+
}
328+
}
329+
330+
// issue #7232
331+
pub struct AsyncOptionLen;
332+
impl AsyncOptionLen {
333+
async fn async_task(&self) -> bool {
334+
true
335+
}
336+
337+
pub async fn len(&self) -> Result<usize, ()> {
338+
Err(())
339+
}
340+
341+
pub async fn is_empty(&self) -> bool {
342+
true
343+
}
344+
}
345+
346+
pub struct AsyncLenSyncIsEmpty;
347+
impl AsyncLenSyncIsEmpty {
348+
pub async fn len(&self) -> u32 {
349+
0
350+
}
351+
352+
pub fn is_empty(&self) -> bool {
353+
true
354+
}
355+
}
356+
357+
// issue #9520
358+
pub struct NonStandardLen;
359+
impl NonStandardLen {
360+
// don't lint
361+
pub fn len(&self, something: usize) -> usize {
362+
something
363+
}
364+
}
365+
285366
// issue #9520
286367
pub struct NonStandardLenAndIsEmptySignature;
287368
impl NonStandardLenAndIsEmptySignature {
@@ -328,4 +409,15 @@ impl NonStandardSignatureWithGenerics {
328409
}
329410
}
330411

412+
pub struct DifferingErrors;
413+
impl DifferingErrors {
414+
pub fn len(&self) -> Result<usize, u8> {
415+
Ok(0)
416+
}
417+
418+
pub fn is_empty(&self) -> Result<bool, u16> {
419+
Ok(true)
420+
}
421+
}
422+
331423
fn main() {}

tests/ui/len_without_is_empty.stderr

+19-1
Original file line numberDiff line numberDiff line change
@@ -119,5 +119,23 @@ LL | pub fn len(&self) -> Result<usize, ()> {
119119
|
120120
= help: use a custom `Error` type instead
121121

122-
error: aborting due to 12 previous errors
122+
error: struct `AsyncLenWithoutIsEmpty` has a public `len` method, but no `is_empty` method
123+
--> $DIR/len_without_is_empty.rs:292:5
124+
|
125+
LL | pub async fn len(&self) -> usize {
126+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
127+
128+
error: struct `AsyncOptionLenWithoutIsEmpty` has a public `len` method, but no `is_empty` method
129+
--> $DIR/len_without_is_empty.rs:304:5
130+
|
131+
LL | pub async fn len(&self) -> Option<usize> {
132+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
133+
134+
error: struct `AsyncResultLenWithoutIsEmpty` has a public `len` method, but no `is_empty` method
135+
--> $DIR/len_without_is_empty.rs:325:5
136+
|
137+
LL | pub async fn len(&self) -> Result<usize, ()> {
138+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
139+
140+
error: aborting due to 15 previous errors
123141

0 commit comments

Comments
 (0)