Skip to content

Commit

Permalink
Rename to return-in-generator
Browse files Browse the repository at this point in the history
  • Loading branch information
charliermarsh committed May 31, 2024
1 parent 9065fe5 commit f19809c
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 31 deletions.
4 changes: 2 additions & 2 deletions crates/ruff_linter/src/checkers/ast/analyze/statement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,8 +207,8 @@ pub(crate) fn statement(stmt: &Stmt, checker: &mut Checker) {
if checker.enabled(Rule::MutableArgumentDefault) {
flake8_bugbear::rules::mutable_argument_default(checker, function_def);
}
if checker.enabled(Rule::ReturnXInGenerator) {
flake8_bugbear::rules::return_x_in_generator(checker, function_def);
if checker.enabled(Rule::ReturnInGenerator) {
flake8_bugbear::rules::return_in_generator(checker, function_def);
}
if checker.any_enabled(&[
Rule::UnnecessaryReturnNone,
Expand Down
2 changes: 1 addition & 1 deletion crates/ruff_linter/src/codes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,7 @@ pub fn code_to_rule(linter: Linter, code: &str) -> Option<(RuleGroup, Rule)> {
(Flake8Bugbear, "033") => (RuleGroup::Stable, rules::flake8_bugbear::rules::DuplicateValue),
(Flake8Bugbear, "034") => (RuleGroup::Stable, rules::flake8_bugbear::rules::ReSubPositionalArgs),
(Flake8Bugbear, "035") => (RuleGroup::Stable, rules::flake8_bugbear::rules::StaticKeyDictComprehension),
(Flake8Bugbear, "901") => (RuleGroup::Preview, rules::flake8_bugbear::rules::ReturnXInGenerator),
(Flake8Bugbear, "901") => (RuleGroup::Preview, rules::flake8_bugbear::rules::ReturnInGenerator),
(Flake8Bugbear, "904") => (RuleGroup::Stable, rules::flake8_bugbear::rules::RaiseWithoutFromInsideExcept),
(Flake8Bugbear, "905") => (RuleGroup::Stable, rules::flake8_bugbear::rules::ZipWithoutExplicitStrict),
(Flake8Bugbear, "909") => (RuleGroup::Preview, rules::flake8_bugbear::rules::LoopIteratorMutation),
Expand Down
2 changes: 1 addition & 1 deletion crates/ruff_linter/src/rules/flake8_bugbear/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ mod tests {
#[test_case(Rule::UselessContextlibSuppress, Path::new("B022.py"))]
#[test_case(Rule::UselessExpression, Path::new("B018.ipynb"))]
#[test_case(Rule::UselessExpression, Path::new("B018.py"))]
#[test_case(Rule::ReturnXInGenerator, Path::new("B901.py"))]
#[test_case(Rule::ReturnInGenerator, Path::new("B901.py"))]
#[test_case(Rule::LoopIteratorMutation, Path::new("B909.py"))]
fn rules(rule_code: Rule, path: &Path) -> Result<()> {
let snapshot = format!("{}_{}", rule_code.noqa_code(), path.to_string_lossy());
Expand Down
4 changes: 2 additions & 2 deletions crates/ruff_linter/src/rules/flake8_bugbear/rules/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ pub(crate) use raise_literal::*;
pub(crate) use raise_without_from_inside_except::*;
pub(crate) use re_sub_positional_args::*;
pub(crate) use redundant_tuple_in_exception_handler::*;
pub(crate) use return_x_in_generator::*;
pub(crate) use return_in_generator::*;
pub(crate) use reuse_of_groupby_generator::*;
pub(crate) use setattr_with_constant::*;
pub(crate) use star_arg_unpacking_after_keyword_arg::*;
Expand Down Expand Up @@ -57,7 +57,7 @@ mod raise_literal;
mod raise_without_from_inside_except;
mod re_sub_positional_args;
mod redundant_tuple_in_exception_handler;
mod return_x_in_generator;
mod return_in_generator;
mod reuse_of_groupby_generator;
mod setattr_with_constant;
mod star_arg_unpacking_after_keyword_arg;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,22 +30,40 @@ use crate::checkers::ast::Checker;
/// yield 1
/// ```
#[violation]
pub struct ReturnXInGenerator;
pub struct ReturnInGenerator;

impl Violation for ReturnXInGenerator {
impl Violation for ReturnInGenerator {
#[derive_message_formats]
fn message(&self) -> String {
format!("Using `yield` together with `return x`. Use native `async def` coroutines or put a `# noqa` comment on this line if this was intentional.")
format!("Using `yield` together with `return`. Use native `async def` coroutines or put a `# noqa` comment on this line if this was intentional.")
}
}

/// B901
pub(crate) fn return_in_generator(checker: &mut Checker, function_def: &StmtFunctionDef) {
if function_def.name.id == "__await__" {
return;
}

let mut visitor = ReturnInGeneratorVisitor::default();
visitor.visit_body(&function_def.body);

if visitor.has_yield {
if let Some(return_) = visitor.return_ {
checker
.diagnostics
.push(Diagnostic::new(ReturnInGenerator, return_));
}
}
}

#[derive(Default)]
struct ReturnXInGeneratorVisitor {
struct ReturnInGeneratorVisitor {
return_: Option<TextRange>,
has_yield: bool,
}

impl Visitor<'_> for ReturnXInGeneratorVisitor {
impl Visitor<'_> for ReturnInGeneratorVisitor {
fn visit_stmt(&mut self, stmt: &Stmt) {
match stmt {
Stmt::Expr(ast::StmtExpr { value, .. }) => match **value {
Expand All @@ -67,21 +85,3 @@ impl Visitor<'_> for ReturnXInGeneratorVisitor {
}
}
}

/// B901
pub(crate) fn return_x_in_generator(checker: &mut Checker, function_def: &StmtFunctionDef) {
if function_def.name.id == "__await__" {
return;
}

let mut visitor = ReturnXInGeneratorVisitor::default();
visitor.visit_body(&function_def.body);

if visitor.has_yield {
if let Some(return_) = visitor.return_ {
checker
.diagnostics
.push(Diagnostic::new(ReturnXInGenerator, return_));
}
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
source: crates/ruff_linter/src/rules/flake8_bugbear/mod.rs
---
B901.py:9:9: B901 Using `yield` together with `return x`. Use native `async def` coroutines or put a `# noqa` comment on this line if this was intentional.
B901.py:9:9: B901 Using `yield` together with `return`. Use native `async def` coroutines or put a `# noqa` comment on this line if this was intentional.
|
7 | def broken():
8 | if True:
Expand All @@ -11,7 +11,7 @@ B901.py:9:9: B901 Using `yield` together with `return x`. Use native `async def`
11 | yield 3
|

B901.py:36:5: B901 Using `yield` together with `return x`. Use native `async def` coroutines or put a `# noqa` comment on this line if this was intentional.
B901.py:36:5: B901 Using `yield` together with `return`. Use native `async def` coroutines or put a `# noqa` comment on this line if this was intentional.
|
35 | def broken2():
36 | return [3, 2, 1]
Expand Down

0 comments on commit f19809c

Please sign in to comment.