Repository: wemake-services/wemake-python-styleguide

Issue:
`match` + `case` does not increase cognitive complexity


`case` in `match` do the same as `if` does. We need to count each `case` as well. We need to add `ast.match_case` to `_CONTROL_FLOW_BREAKERS`. See https://github.com/wemake-services/wemake-python-styleguide/blob/a47422db1e4261b8be23dd2d015d0db977100b53/wemake_python_styleguide/logic/complexity/cognitive.py#L25-L31

We also need to add a test case for this to https://github.com/wemake-services/wemake-python-styleguide/blob/master/tests/test_logic/test_complexity/test_cognitive/test_cognitive_complexity.py


Discussion:

Patch:
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 91bdc3417..538a5b422 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -133,6 +133,7 @@ Semantic versioning in our case means:
 - Fixes several violations not been detected in `case:` statements
 - Fixes `WPS434` not detecting reassignments in `:=` case
 - Fixes `WPS314` not detecting `match` statements
+- Fixes `match` + `case` does not increase cognitive complexity
 
 ### Misc
 
diff --git a/wemake_python_styleguide/logic/complexity/cognitive.py b/wemake_python_styleguide/logic/complexity/cognitive.py
index 7843668e6..43b1d0b53 100644
--- a/wemake_python_styleguide/logic/complexity/cognitive.py
+++ b/wemake_python_styleguide/logic/complexity/cognitive.py
@@ -28,6 +28,7 @@
     ast.AsyncFor,
     ast.While,
     ast.IfExp,
+    ast.match_case,
 )
 
 #: Control flow nodes that increment.


Test patch:
diff --git a/tests/test_logic/test_complexity/test_cognitive/test_cognitive_complexity.py b/tests/test_logic/test_complexity/test_cognitive/test_cognitive_complexity.py
index 6331fbe4e..b7ae9cb7c 100644
--- a/tests/test_logic/test_complexity/test_cognitive/test_cognitive_complexity.py
+++ b/tests/test_logic/test_complexity/test_cognitive/test_cognitive_complexity.py
@@ -23,6 +23,12 @@ def f(self, a):
         return a * self.f(a - 1)  # +1 for recursion
 """
 
+complexity1_4 = """
+def f(a):
+    match a:
+        case _: return a # +1
+"""
+
 complexity2_1 = """
 def f(a, b):
     if a and b and True:  # +2
@@ -37,6 +43,13 @@ def f(a, b):
         return 2
 """
 
+complexity2_3 = """
+def f(a, b):
+    match a:
+        case 1: return 1  # +1
+        case 2: return 2  # +1
+"""
+
 complexity3_1 = """
 def f(a, b):
     if a and b or True:  # +3
@@ -112,6 +125,15 @@ def f(a):
             break  # +2
 """
 
+complexity9_2 = """
+def f(a):
+    for a in range(10):  # +1
+        match a:  # +2
+            case 1: continue  # +3
+            case 2: break  # +3
+"""
+
+
 complexity10_1 = """
 def process_raw_constant(constant, min_word_length):
     processed_words = []
@@ -150,8 +172,10 @@ def enhance(tree):
         (complexity1_1, 1),
         (complexity1_2, 1),
         (complexity1_3, 1),
+        (complexity1_4, 1),
         (complexity2_1, 2),
         (complexity2_2, 2),
+        (complexity2_3, 2),
         (complexity3_1, 3),
         (complexity3_2, 3),
         (complexity3_3, 3),
@@ -161,6 +185,7 @@ def enhance(tree):
         (complexity5_1, 5),
         (complexity6_1, 6),
         (complexity9_1, 9),
+        (complexity9_2, 9),
         (complexity10_1, 10),
         (complexity14_1, 14),
     ],