Skip to content

Commit bb48a09

Browse files
committed
Use a single field for tracking return statements
1 parent c59e02b commit bb48a09

1 file changed

Lines changed: 31 additions & 19 deletions

File tree

crates/ruff_linter/src/rules/pydoclint/rules/check_docstring.rs

Lines changed: 31 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -495,13 +495,26 @@ fn parse_entries_numpy(content: &str) -> Vec<QualifiedName> {
495495
entries
496496
}
497497

498-
/// An individual documentable statement in a function body.
498+
/// An individual `yield` expression in a function body.
499499
#[derive(Debug)]
500-
struct Entry {
500+
struct YieldEntry {
501501
range: TextRange,
502502
}
503503

504-
impl Ranged for Entry {
504+
impl Ranged for YieldEntry {
505+
fn range(&self) -> TextRange {
506+
self.range
507+
}
508+
}
509+
510+
/// An individual `return` statement in a function body.
511+
#[derive(Debug)]
512+
struct ReturnEntry {
513+
range: TextRange,
514+
is_none_return: bool,
515+
}
516+
517+
impl Ranged for ReturnEntry {
505518
fn range(&self) -> TextRange {
506519
self.range
507520
}
@@ -523,17 +536,15 @@ impl Ranged for ExceptionEntry<'_> {
523536
/// A summary of documentable statements from the function body
524537
#[derive(Debug)]
525538
struct BodyEntries<'a> {
526-
returns: Vec<Entry>,
527-
none_returns: usize,
528-
yields: Vec<Entry>,
539+
returns: Vec<ReturnEntry>,
540+
yields: Vec<YieldEntry>,
529541
raised_exceptions: Vec<ExceptionEntry<'a>>,
530542
}
531543

532544
/// An AST visitor to extract a summary of documentable statements from a function body.
533545
struct BodyVisitor<'a> {
534-
returns: Vec<Entry>,
535-
none_returns: usize,
536-
yields: Vec<Entry>,
546+
returns: Vec<ReturnEntry>,
547+
yields: Vec<YieldEntry>,
537548
currently_suspended_exceptions: Option<&'a ast::Expr>,
538549
raised_exceptions: Vec<ExceptionEntry<'a>>,
539550
semantic: &'a SemanticModel<'a>,
@@ -543,7 +554,6 @@ impl<'a> BodyVisitor<'a> {
543554
fn new(semantic: &'a SemanticModel) -> Self {
544555
Self {
545556
returns: Vec::new(),
546-
none_returns: 0,
547557
yields: Vec::new(),
548558
currently_suspended_exceptions: None,
549559
raised_exceptions: Vec::new(),
@@ -554,7 +564,6 @@ impl<'a> BodyVisitor<'a> {
554564
fn finish(self) -> BodyEntries<'a> {
555565
let BodyVisitor {
556566
returns,
557-
none_returns,
558567
yields,
559568
mut raised_exceptions,
560569
..
@@ -576,7 +585,6 @@ impl<'a> BodyVisitor<'a> {
576585

577586
BodyEntries {
578587
returns,
579-
none_returns,
580588
yields,
581589
raised_exceptions,
582590
}
@@ -631,10 +639,10 @@ impl<'a> Visitor<'a> for BodyVisitor<'a> {
631639
range,
632640
value: Some(value),
633641
}) => {
634-
self.returns.push(Entry { range: *range });
635-
if value.is_none_literal_expr() {
636-
self.none_returns += 1;
637-
}
642+
self.returns.push(ReturnEntry {
643+
range: *range,
644+
is_none_return: value.is_none_literal_expr(),
645+
});
638646
}
639647
Stmt::FunctionDef(_) | Stmt::ClassDef(_) => return,
640648
_ => {}
@@ -649,10 +657,10 @@ impl<'a> Visitor<'a> for BodyVisitor<'a> {
649657
range,
650658
value: Some(_),
651659
}) => {
652-
self.yields.push(Entry { range: *range });
660+
self.yields.push(YieldEntry { range: *range });
653661
}
654662
Expr::YieldFrom(ast::ExprYieldFrom { range, .. }) => {
655-
self.yields.push(Entry { range: *range });
663+
self.yields.push(YieldEntry { range: *range });
656664
}
657665
Expr::Lambda(_) => return,
658666
_ => {}
@@ -750,7 +758,11 @@ pub(crate) fn check_docstring(
750758
Some(returns) if !Expr::is_none_literal_expr(returns) => diagnostics.push(
751759
Diagnostic::new(DocstringMissingReturns, body_return.range()),
752760
),
753-
None if body_entries.returns.len() != body_entries.none_returns => {
761+
None if body_entries
762+
.returns
763+
.iter()
764+
.any(|entry| !entry.is_none_return) =>
765+
{
754766
diagnostics.push(Diagnostic::new(
755767
DocstringMissingReturns,
756768
body_return.range(),

0 commit comments

Comments
 (0)