From 6907df489b06fd6930a1d1e989eeb3f2e97a5ff7 Mon Sep 17 00:00:00 2001 From: Charlie Marsh Date: Thu, 22 Dec 2022 10:56:04 -0500 Subject: [PATCH] Extend false-positive list for flake8-boolean-trap (#1338) --- .../test/fixtures/flake8_boolean_trap/FBT.py | 2 ++ src/flake8_boolean_trap/plugins.rs | 20 +++++++++++++------ 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/resources/test/fixtures/flake8_boolean_trap/FBT.py b/resources/test/fixtures/flake8_boolean_trap/FBT.py index 3be547fde9..3e48f10cf8 100644 --- a/resources/test/fixtures/flake8_boolean_trap/FBT.py +++ b/resources/test/fixtures/flake8_boolean_trap/FBT.py @@ -55,3 +55,5 @@ a.get("hello", False) {}.pop(True, False) dict.fromkeys(("world",), True) {}.deploy(True, False) +getattr(someobj, attrname, False) +mylist.index(True) diff --git a/src/flake8_boolean_trap/plugins.rs b/src/flake8_boolean_trap/plugins.rs index fe6b5f866a..9fa9f62dca 100644 --- a/src/flake8_boolean_trap/plugins.rs +++ b/src/flake8_boolean_trap/plugins.rs @@ -14,7 +14,10 @@ const FUNC_NAME_ALLOWLIST: &[&str] = &[ "failUnlessEqual", "fromkeys", "get", + "getattr", + "index", "pop", + "setattr", "setdefault", ]; @@ -22,10 +25,15 @@ const FUNC_NAME_ALLOWLIST: &[&str] = &[ /// `true`, the function name must be explicitly allowed, and the argument must /// be either the first or second argument in the call. fn allow_boolean_trap(func: &Expr) -> bool { - let ExprKind::Attribute { attr, .. } = &func.node else { - return false; - }; - FUNC_NAME_ALLOWLIST.contains(&attr.as_ref()) + if let ExprKind::Attribute { attr, .. } = &func.node { + return FUNC_NAME_ALLOWLIST.contains(&attr.as_ref()); + } + + if let ExprKind::Name { id, .. } = &func.node { + return FUNC_NAME_ALLOWLIST.contains(&id.as_ref()); + } + + false } fn is_boolean_arg(arg: &Expr) -> bool { @@ -90,8 +98,8 @@ pub fn check_boolean_positional_value_in_function_call( args: &[Expr], func: &Expr, ) { - for (index, arg) in args.iter().enumerate() { - if index < 2 && allow_boolean_trap(func) { + for arg in args { + if allow_boolean_trap(func) { continue; } add_if_boolean(