Skip to content

Commit

Permalink
SONARPY-1536 Fix false positive for Rule S6735. (#1624)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeremi Do Dinh authored Oct 30, 2023
1 parent e710dfc commit 1ebb12e
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
package org.sonar.python.checks;

import java.util.ArrayList;
import java.util.EnumSet;
import java.util.List;
import java.util.Map;
import java.util.Optional;
Expand All @@ -42,7 +43,9 @@ public class PandasAddMergeParametersCheck extends PythonSubscriptionCheck {
enum Keywords {
HOW("how", 2, 1, 2, "\"inner\"", "\"left\""),
ON("on", 1, 2, 3, "None", "None"),
VALIDATE("validate", 6, 11, 12, "\"many_to_many\"", "\"many_to_many\"");
VALIDATE("validate", 6, 11, 12, "\"many_to_many\"", "\"many_to_many\""),
LEFT_ON("left_on", -1, 3, 4, "None", "None"),
RIGHT_ON("right_on", -1, 4, 5, "None", "None");

public String getKeyword() {
return keyword;
Expand Down Expand Up @@ -99,6 +102,7 @@ int getArgumentPosition(String fullyQualifiedName) {
private static final String DATAFRAME_MERGE_FQN = "pandas.core.frame.DataFrame.merge";
private static final String PANDAS_MERGE_FQN = "pandas.core.reshape.merge.merge";

private static final Set<Keywords> ON_KEYWORDS = EnumSet.of(Keywords.ON, Keywords.LEFT_ON, Keywords.RIGHT_ON);
private static final Set<String> METHODS = Set.of(
DATAFRAME_JOIN_FQN,
DATAFRAME_MERGE_FQN,
Expand Down Expand Up @@ -129,7 +133,7 @@ private static void missingArguments(String fullyQualifiedName, SubscriptionCont
if (isArgumentMissing(fullyQualifiedName, Keywords.HOW, callExpression.arguments())) {
missingKeywords.add(Keywords.HOW);
}
if (isArgumentMissing(fullyQualifiedName, Keywords.ON, callExpression.arguments())) {
if (ON_KEYWORDS.stream().allMatch(keyword -> isArgumentMissing(fullyQualifiedName, keyword, callExpression.arguments()))) {
missingKeywords.add(Keywords.ON);
}
if (isArgumentMissing(fullyQualifiedName, Keywords.VALIDATE, callExpression.arguments())) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,22 @@ def compliant_1(xx):
_ = age_df.merge(name_df, "inner", "user_id", None, None, False, False, False, ('_x', '_y'), None, False, "1:1")

_ = age_df.join(name_df, on=None, how='left', lsuffix='', rsuffix='', sort=False, validate=None)

_ = pd.merge(age_df, name_df, left_on=col, right_on='cat', how='left', validate='m:m')

_ = age_df.merge(name_df, left_on=col, right_on='cat', how='left', validate='m:m')

_ = age_df.merge(name_df, left_on=col, right_on='cat', how='left', validate='m:m')

_ = pd.merge(age_df, name_df, right_on='cat', how='left', validate='m:m')

_ = age_df.merge(name_df, right_on='cat', how='left', validate='m:m')

_ = age_df.merge(name_df, right_on='cat', how='left', validate='m:m')

_ = pd.merge(age_df, name_df, left_on=col, how='left', validate='m:m')

_ = age_df.merge(name_df, left_on=col, how='left', validate='m:m')

_ = age_df.merge(name_df, left_on=col, how='left', validate='m:m')

0 comments on commit 1ebb12e

Please sign in to comment.