Skip to content

Commit 7667f77

Browse files
Seppli11sonartech
authored andcommitted
SONARPY-3178 Convert cursor instructions to github instructions (#389)
GitOrigin-RevId: b41ec5c2144295cfba70211cb75ce4b88f89e366
1 parent 48b49f2 commit 7667f77

11 files changed

Lines changed: 909 additions & 0 deletions
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
---
2+
description: "Reference for CheckUtils utility methods."
3+
applyTo: "**/*checks*/**/*.java"
4+
---
5+
# CheckUtils Reference
6+
7+
Import :
8+
```java
9+
import org.sonar.python.checks.utils.CheckUtils;
10+
```
11+
12+
## Public Static Methods
13+
14+
- boolean areEquivalent(Tree leftTree, Tree rightTree) : Compares two AST subtrees for structural and token-value equivalence; useful in rules or tests to detect duplicate or matching nodes.
15+
- ClassDef getParentClassDef(Tree tree) : Finds the nearest enclosing class definition or returns null if none; used to determine the class context for a given AST node.
16+
- boolean classHasInheritance(ClassDef classDef) : Checks if a class declares any base other than the default `object`; used to detect or enforce explicit inheritance patterns.
17+
- boolean containsCallToLocalsFunction(Tree tree) : Determines if a subtree contains any call to `locals()`; helpful in rules detecting dynamic locals usage.
18+
- boolean isConstant(Expression condition) : Checks if an expression is an immutable literal or a literal collection without unpacking; used to identify compile-time constant conditions in checks.
19+
- boolean isImmutableConstant(Expression condition) : Returns true for boolean, numeric, string literals, `None`, lambdas, or generator expressions; useful for primitive constant detection.
20+
- boolean isConstantCollectionLiteral(Expression condition) : Returns true for list, dict, set, or tuple literals without unpacking; used to detect static collection declarations in code.
21+
- boolean mustBeAProtocolLike(ClassDef classDef) : Determines if a class extends a `typing.Protocol` or similar interface; used in protocol-specific or interface checks.
22+
- boolean isAbstract(FunctionDef funDef) : Checks whether a function is decorated as an abstract method; helpful in analyzing abstract base classes.
23+
- boolean isEmptyStatement(Statement statement) : Identifies `pass` statements, standalone string literals, or ellipsis as empty/no-op statements; used to skip over no-op code.
24+
- boolean isSelf(Expression expression) : Checks if an expression is the name `self`; useful to special-case instance references in method checks.
25+
- Symbol findFirstParameterSymbol(FunctionDef functionDef) : Retrieves the symbol of the first parameter or returns null; used in parameter-name based analyses and naming rules.
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
---
2+
description: ""
3+
applyTo: "**/*checks*/**/*.java"
4+
---
5+
# CommonValidationUtils Reference
6+
7+
Import:
8+
```java
9+
import org.sonar.python.checks.hotspots.CommonValidationUtils;
10+
```
11+
12+
## Public Static Methods
13+
14+
- boolean isMoreThan(Expression expression, int number) : Checks if an expression's numeric value is strictly greater than the given threshold, resolving name indirections; typical use: detecting sleep durations exceeding a limit (e.g., in `AsyncLongSleepCheck`).
15+
16+
- boolean isEqualTo(Expression expression, int number) : Determines if an expression represents a numeric literal equal to a specified value (integer or double), resolving name indirections; typical use: validating exact numeric matches in code analysis.
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
---
2+
applyTo: "**/*checks*/**/*.java"
3+
---
4+
# Expressions Utility Reference
5+
6+
Import:
7+
```java
8+
import org.sonar.python.checks.utils.Expressions;
9+
```
10+
11+
Public Static Methods:
12+
13+
- boolean isFalsy(@Nullable Expression expression): Checks if an expression is definitely falsy (None, False literal, zero, empty string/list/tuple/dict); typical use to detect statically false conditions.
14+
- boolean isTruthy(@Nullable Expression expression): Checks if an expression is definitely truthy (True literal, non-zero numeric, non-empty literal); use for static truth-value testing.
15+
- @CheckForNull Expression singleAssignedValue(Name name): Returns the single assigned value for a variable if assigned exactly once; useful to resolve simple variable indirections.
16+
- Expression removeParentheses(Expression expression): Strips enclosing parentheses from an expression; handy to simplify AST node analysis.
17+
- Optional<Expression> singleAssignedNonNameValue(Name name): Resolves a variable to its non-name assigned value, skipping name-only indirections; useful for inlining constant or literal values.
18+
- Optional<Expression> ifNameGetSingleAssignedNonNameValue(Expression expression): If expression is a name, returns its underlying non-name assigned value; otherwise returns the original; for unified value resolution.
19+
- List<Expression> expressionsFromListOrTuple(Expression expression): Extracts elements from a ListLiteral or Tuple node; use to iterate literal collections in code.
20+
- String unescape(StringLiteral stringLiteral): Concatenates and unescapes parts of a string literal into its runtime value; use to get actual string content.
21+
- String unescapeString(String value, boolean isBytesLiteral): Unescapes escape sequences in a raw string value, considering bytes vs string semantics; visible for testing literal unescaping.
22+
- Optional<Name> getAssignedName(Expression expression): Finds the single name to which an expression is assigned, with limited recursion; useful for tracking simple assignments.
23+
- List<Expression> getExpressionsFromRhs(Expression rhs): Gathers expressions from RHS of tuple/list/unpacking assignments; use in multi-variable assignment analysis.
24+
- boolean isGenericTypeAnnotation(Expression expression): Detects calls to typing.TypeVar (generic type annotations); use to identify TypeVar usage in type declarations.
25+
- boolean containsSpreadOperator(List<Argument> arguments): Returns true if any argument uses unpacking (`*` or `**`); use to detect spread operator in call arguments.

0 commit comments

Comments
 (0)