use of org.sonar.plugins.python.api.tree.UnpackingExpression in project sonar-python by SonarSource.
the class DuplicateArgumentCheck method extractKeysFromDictionary.
private static Set<String> extractKeysFromDictionary(UnpackingExpression unpackingExpression) {
if (unpackingExpression.expression().is(Tree.Kind.DICTIONARY_LITERAL)) {
return keysInDictionaryLiteral((DictionaryLiteral) unpackingExpression.expression());
} else if (unpackingExpression.expression().is(Tree.Kind.CALL_EXPR)) {
return keysFromDictionaryCreation((CallExpression) unpackingExpression.expression());
} else if (unpackingExpression.expression().is(Tree.Kind.NAME)) {
Name name = (Name) unpackingExpression.expression();
Symbol symbol = name.symbol();
if (symbol == null || symbol.usages().stream().anyMatch(u -> TreeUtils.firstAncestorOfKind(u.tree(), Tree.Kind.DEL_STMT) != null)) {
return Collections.emptySet();
}
Expression expression = Expressions.singleAssignedValue(name);
if (expression != null && expression.is(Tree.Kind.CALL_EXPR)) {
return keysFromDictionaryCreation((CallExpression) expression);
}
return expression != null && expression.is(Tree.Kind.DICTIONARY_LITERAL) ? keysInDictionaryLiteral((DictionaryLiteral) expression) : Collections.emptySet();
}
return Collections.emptySet();
}
use of org.sonar.plugins.python.api.tree.UnpackingExpression in project sonar-python by SonarSource.
the class DuplicateArgumentCheck method checkFunctionCall.
private static void checkFunctionCall(CallExpression callExpression, FunctionSymbol functionSymbol, int firstParameterOffset, SubscriptionContext ctx) {
Map<String, List<Tree>> passedParameters = new HashMap<>();
List<FunctionSymbol.Parameter> parameters = functionSymbol.parameters();
List<Argument> arguments = callExpression.arguments();
for (int i = 0; i < arguments.size(); i++) {
Argument argument = arguments.get(i);
if (argument.is(Tree.Kind.REGULAR_ARGUMENT)) {
RegularArgument regularArgument = (RegularArgument) argument;
int parameterIndex = i + firstParameterOffset;
boolean shouldAbortCheck = checkRegularArgument(regularArgument, parameters, parameterIndex, passedParameters);
if (shouldAbortCheck) {
return;
}
} else {
UnpackingExpression unpackingExpression = (UnpackingExpression) argument;
boolean isDictionary = unpackingExpression.starToken().type().equals(PythonPunctuator.MUL_MUL);
if (isDictionary) {
checkDictionary(unpackingExpression, passedParameters);
} else {
// No issue raised for unpacked positional arguments
return;
}
}
}
reportIssues(passedParameters, functionSymbol, ctx);
}
use of org.sonar.plugins.python.api.tree.UnpackingExpression in project sonar-python by SonarSource.
the class IterationOnNonIterable method checkUnpackingExpression.
private void checkUnpackingExpression(SubscriptionContext ctx) {
UnpackingExpression unpackingExpression = (UnpackingExpression) ctx.syntaxNode();
if (unpackingExpression.starToken().type().equals(PythonPunctuator.MUL_MUL)) {
return;
}
Expression expression = unpackingExpression.expression();
Map<LocationInFile, String> secondaries = new HashMap<>();
if (!isValidIterable(expression, secondaries)) {
reportIssue(ctx, expression, secondaries, message(expression, false));
}
}
use of org.sonar.plugins.python.api.tree.UnpackingExpression in project sonar-python by SonarSource.
the class PythonTreeMakerTest method dictionary_literal.
@Test
public void dictionary_literal() {
setRootRule(PythonGrammar.ATOM);
DictionaryLiteral tree = (DictionaryLiteral) parse("{'key': 'value'}", treeMaker::expression);
assertThat(tree.firstToken().value()).isEqualTo("{");
assertThat(tree.lastToken().value()).isEqualTo("}");
assertThat(tree.getKind()).isEqualTo(Tree.Kind.DICTIONARY_LITERAL);
assertThat(tree.elements()).hasSize(1);
KeyValuePair keyValuePair = (KeyValuePair) tree.elements().iterator().next();
assertThat(keyValuePair.getKind()).isEqualTo(Tree.Kind.KEY_VALUE_PAIR);
assertThat(keyValuePair.key().getKind()).isEqualTo(Tree.Kind.STRING_LITERAL);
assertThat(keyValuePair.colon().value()).isEqualTo(":");
assertThat(keyValuePair.value().getKind()).isEqualTo(Tree.Kind.STRING_LITERAL);
assertThat(tree.children()).hasSize(3).containsExactly(tree.lCurlyBrace(), tree.elements().get(0), tree.rCurlyBrace());
tree = (DictionaryLiteral) parse("{'key': 'value', 'key2': 'value2'}", treeMaker::expression);
assertThat(tree.elements()).hasSize(2);
assertThat(tree.children()).hasSize(5).containsExactly(tree.lCurlyBrace(), tree.elements().get(0), tree.commas().get(0), tree.elements().get(1), tree.rCurlyBrace());
tree = (DictionaryLiteral) parse("{** var}", treeMaker::expression);
assertThat(tree.elements()).hasSize(1);
UnpackingExpression dictUnpacking = (UnpackingExpression) tree.elements().iterator().next();
assertThat(dictUnpacking.expression().getKind()).isEqualTo(Tree.Kind.NAME);
assertThat(dictUnpacking.starToken().value()).isEqualTo("**");
tree = (DictionaryLiteral) parse("{** var, key: value}", treeMaker::expression);
assertThat(tree.elements()).hasSize(2);
}
use of org.sonar.plugins.python.api.tree.UnpackingExpression in project sonar-python by SonarSource.
the class PythonTreeMakerTest method argument.
@Test
public void argument() {
setRootRule(PythonGrammar.ARGUMENT);
RegularArgument argumentTree = (RegularArgument) parse("foo", treeMaker::argument);
assertThat(argumentTree.equalToken()).isNull();
assertThat(argumentTree.keywordArgument()).isNull();
Name name = (Name) argumentTree.expression();
assertThat(name.name()).isEqualTo("foo");
assertThat(argumentTree.children()).hasSize(1);
UnpackingExpression iterableUnpacking = (UnpackingExpression) parse("*foo", treeMaker::argument);
name = (Name) iterableUnpacking.expression();
assertThat(name.name()).isEqualTo("foo");
assertThat(iterableUnpacking.children()).hasSize(2);
UnpackingExpression dictionaryUnpacking = (UnpackingExpression) parse("**foo", treeMaker::argument);
name = (Name) dictionaryUnpacking.expression();
assertThat(name.name()).isEqualTo("foo");
assertThat(dictionaryUnpacking.starToken()).isNotNull();
assertThat(dictionaryUnpacking.children()).hasSize(2);
argumentTree = (RegularArgument) parse("bar=foo", treeMaker::argument);
assertThat(argumentTree.equalToken()).isNotNull();
Name keywordArgument = argumentTree.keywordArgument();
assertThat(keywordArgument.name()).isEqualTo("bar");
name = (Name) argumentTree.expression();
assertThat(name.name()).isEqualTo("foo");
assertThat(argumentTree.children()).hasSize(3).containsExactly(argumentTree.keywordArgument(), argumentTree.equalToken(), argumentTree.expression());
}
Aggregations