From de307ccf20c69ef1cfba05c000de19ec2407b5cd Mon Sep 17 00:00:00 2001 From: Martin Date: Wed, 3 Nov 2021 22:49:04 +0100 Subject: Inspection for `with threading.Lock():` (#5245) Using `with threading.Lock():` directly has no effect. Correct usage is: ``` lock = threading.Lock() with lock: ... ``` This applies for: * threading.Lock * threading.RLock * threading.Condition * threading.Semaphore * threading.BoundedSemaphore Signed-off-by: Martin Basti --- tests/functional/u/useless/useless_with_lock.py | 58 ++++++++++++++++++++++++ tests/functional/u/useless/useless_with_lock.txt | 11 +++++ 2 files changed, 69 insertions(+) create mode 100644 tests/functional/u/useless/useless_with_lock.py create mode 100644 tests/functional/u/useless/useless_with_lock.txt (limited to 'tests') diff --git a/tests/functional/u/useless/useless_with_lock.py b/tests/functional/u/useless/useless_with_lock.py new file mode 100644 index 000000000..19d664084 --- /dev/null +++ b/tests/functional/u/useless/useless_with_lock.py @@ -0,0 +1,58 @@ +"""Tests for the useless-with-lock message""" +# pylint: disable=missing-docstring +import threading +from threading import Lock, RLock, Condition, Semaphore, BoundedSemaphore + + +with threading.Lock(): # [useless-with-lock] + ... + +with Lock(): # [useless-with-lock] + ... + +with threading.Lock() as this_shouldnt_matter: # [useless-with-lock] + ... + +with threading.RLock(): # [useless-with-lock] + ... + +with RLock(): # [useless-with-lock] + ... + +with threading.Condition(): # [useless-with-lock] + ... + +with Condition(): # [useless-with-lock] + ... + +with threading.Semaphore(): # [useless-with-lock] + ... + +with Semaphore(): # [useless-with-lock] + ... + +with threading.BoundedSemaphore(): # [useless-with-lock] + ... + +with BoundedSemaphore(): # [useless-with-lock] + ... + +lock = threading.Lock() +with lock: # this is ok + ... + +rlock = threading.RLock() +with rlock: # this is ok + ... + +cond = threading.Condition() +with cond: # this is ok + ... + +sem = threading.Semaphore() +with sem: # this is ok + ... + +b_sem = threading.BoundedSemaphore() +with b_sem: # this is ok + ... diff --git a/tests/functional/u/useless/useless_with_lock.txt b/tests/functional/u/useless/useless_with_lock.txt new file mode 100644 index 000000000..51435c35e --- /dev/null +++ b/tests/functional/u/useless/useless_with_lock.txt @@ -0,0 +1,11 @@ +useless-with-lock:7:0::'threading.Lock()' directly created in 'with' has no effect:HIGH +useless-with-lock:10:0::'threading.Lock()' directly created in 'with' has no effect:HIGH +useless-with-lock:13:0::'threading.Lock()' directly created in 'with' has no effect:HIGH +useless-with-lock:16:0::'threading.RLock()' directly created in 'with' has no effect:HIGH +useless-with-lock:19:0::'threading.RLock()' directly created in 'with' has no effect:HIGH +useless-with-lock:22:0::'threading.Condition()' directly created in 'with' has no effect:HIGH +useless-with-lock:25:0::'threading.Condition()' directly created in 'with' has no effect:HIGH +useless-with-lock:28:0::'threading.Semaphore()' directly created in 'with' has no effect:HIGH +useless-with-lock:31:0::'threading.Semaphore()' directly created in 'with' has no effect:HIGH +useless-with-lock:34:0::'threading.BoundedSemaphore()' directly created in 'with' has no effect:HIGH +useless-with-lock:37:0::'threading.BoundedSemaphore()' directly created in 'with' has no effect:HIGH -- cgit v1.2.1