Repository: wemake-services/wemake-python-styleguide

Issue:
Constant condition in comprehension is not reported
Reproduction:

```python
comp = [num for num in range(5) if True]
```


Discussion:
The same happens for:

```python
[
    ...
    for x in ...
    if (some if some > 1 else other)
]
```

It should raise `NestedTernaryViolation`.

Resolving patch:
diff --git a/wemake_python_styleguide/visitors/ast/compares.py b/wemake_python_styleguide/visitors/ast/compares.py
--- a/wemake_python_styleguide/visitors/ast/compares.py
+++ b/wemake_python_styleguide/visitors/ast/compares.py
@@ -304,6 +304,7 @@ class WrongConditionalVisitor(BaseNodeVisitor):
         ast.BinOp,
         ast.UnaryOp,
         ast.Compare,
+        ast.comprehension,
     )
 
     def visit_any_if(self, node: AnyIf) -> None:
@@ -322,14 +323,17 @@ def visit_any_if(self, node: AnyIf) -> None:
             self._check_simplifiable_ifexpr(node)
 
         self._check_nested_ifexpr(node)
-        self._check_constant_condition(node)
+        self._check_constant_condition(node.test)
         self.generic_visit(node)
 
-    def _check_constant_condition(self, node: AnyIf) -> None:
-        real_node = operators.unwrap_unary_node(
-            get_assigned_expr(node.test),
-        )
+    def visit_comprehension(self, node: ast.comprehension) -> None:
+        """Checks all possible comprehensions."""
+        for expr in node.ifs:
+            self._check_constant_condition(expr)
+        self.generic_visit(node)
 
+    def _check_constant_condition(self, node: ast.AST) -> None:
+        real_node = operators.unwrap_unary_node(get_assigned_expr(node))
         if isinstance(real_node, self._forbidden_nodes):
             self.add_violation(ConstantConditionViolation(node))
 


Test patch:
diff --git a/tests/test_visitors/test_ast/test_compares/test_conditionals.py b/tests/test_visitors/test_ast/test_compares/test_conditionals.py
--- a/tests/test_visitors/test_ast/test_compares/test_conditionals.py
+++ b/tests/test_visitors/test_ast/test_compares/test_conditionals.py
@@ -16,16 +16,42 @@
 if_statement = 'if {0}: ...'
 ternary = 'ternary = 0 if {0} else 1'
 
-if_statement_in_comprehension = """
+list_comprehension = """
 def container():
     [x for x in [1, 2, 3] if {0}]
 """
 
+set_comprehension = """
+def container():
+    {{
+        x
+        for x in [1, 2, 3]
+        if {0}
+    }}
+"""
+
+dict_comprehension = """
+def container():
+    {{
+        x: '1'
+        for x in [1, 2, 3]
+        if {0}
+    }}
+"""
+
+gen_comprehension = """
+def container():
+    (x for x in [1, 2, 3] if {0})
+"""
+
 
 @pytest.mark.parametrize('code', [
     if_statement,
     ternary,
-    if_statement_in_comprehension,
+    list_comprehension,
+    set_comprehension,
+    dict_comprehension,
+    gen_comprehension,
 ])
 @pytest.mark.parametrize('comparators', [
     'variable < 3',
@@ -62,6 +88,10 @@ def test_valid_conditional(
 @pytest.mark.parametrize('code', [
     if_statement,
     ternary,
+    # list_comprehension,
+    # set_comprehension,
+    # dict_comprehension,
+    # gen_comprehension,
 ])
 @pytest.mark.parametrize('comparators', [
     'True',
@@ -81,16 +111,23 @@ def test_valid_conditional(
         '(unique := True)',
         marks=pytest.mark.skipif(not PY38, reason='walrus appeared in 3.8'),
     ),
+    pytest.param(
+        '(unique := -1)',
+        marks=pytest.mark.skipif(not PY38, reason='walrus appeared in 3.8'),
+    ),
 ])
-def test_useless(
+def test_constant_condition(
     assert_errors,
     parse_ast_tree,
     code,
     comparators,
     default_options,
+    mode,
 ):
     """Testing that violations are when using invalid conditional."""
-    tree = parse_ast_tree(create_variable.format(code.format(comparators)))
+    tree = parse_ast_tree(
+        mode(create_variable.format(code.format(comparators))),
+    )
 
     visitor = WrongConditionalVisitor(default_options, tree=tree)
     visitor.run()
diff --git a/tests/test_visitors/test_ast/test_compares/test_nested_ternary.py b/tests/test_visitors/test_ast/test_compares/test_nested_ternary.py
--- a/tests/test_visitors/test_ast/test_compares/test_nested_ternary.py
+++ b/tests/test_visitors/test_ast/test_compares/test_nested_ternary.py
@@ -47,6 +47,11 @@
 wrong_if3 = 'if attr.call(a if b else c): ...'
 wrong_if4 = 'if x := 1 if True else 2: ...'
 
+wrong_comprehension1 = '[x for x in number if (some if x else other)]'
+wrong_comprehension2 = '(x for x in number if (some if x else other))'
+wrong_comprehension3 = '{{x for x in number if (some if x else other)}}'
+wrong_comprehension4 = '{{x: 1 for x in number if (some if x else other)}}'
+
 # Correct:
 
 correct_if1 = """
@@ -157,6 +162,11 @@ def test_non_nested_ternary(
         wrong_if3,
         marks=pytest.mark.skipif(not PY38, reason='walrus appeared in 3.8'),
     ),
+
+    wrong_comprehension1,
+    wrong_comprehension2,
+    wrong_comprehension3,
+    wrong_comprehension4,
 ])
 def test_nested_ternary(
     assert_errors,
diff --git a/tests/test_visitors/test_tokenize/test_statements/test_inconsistent_comprehension.py b/tests/test_visitors/test_tokenize/test_statements/test_inconsistent_comprehension.py
--- a/tests/test_visitors/test_tokenize/test_statements/test_inconsistent_comprehension.py
+++ b/tests/test_visitors/test_tokenize/test_statements/test_inconsistent_comprehension.py
@@ -389,6 +389,17 @@ def wrapper():
     ]
 """
 
+wrong_list_split_multiple_ifs3 = """
+def wrapper():
+    [
+        some(number)
+        for number in matrix
+        if some
+        for other in iterable
+        if number > 0
+    ]
+"""
+
 wrong_list_two_compehensions1 = """
 def wrapper():
     comp = [
@@ -506,6 +517,7 @@ def wrapper():
     wrong_list_split_for_in,
     wrong_list_split_multiple_ifs1,
     wrong_list_split_multiple_ifs2,
+    wrong_list_split_multiple_ifs3,
     wrong_list_two_compehensions1,
     wrong_list_two_compehensions2,
     wrong_dict_almost_one_line,