Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SONARJAVA-5149 S1075: find when uri is short and relative and stop raising issues #4961

Merged
merged 11 commits into from
Dec 13, 2024

Conversation

erwan-serandour
Copy link
Contributor

@erwan-serandour erwan-serandour commented Dec 12, 2024

@hashicorp-vault-sonar-prod hashicorp-vault-sonar-prod bot changed the title SONARJAVA-5149 S1075: find when uri is short and relative and stop raising issues SONARJAVA-5149 S1075: find when uri is short and relative and stop raising issues Dec 12, 2024
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll left a few comments, but it's end of the day, so I might leave some more in the second round.

@@ -45,8 +45,8 @@ void foo(String s, String var) throws URISyntaxException {
// ^^^^^^^^^^

String filename;
String path = "/home/path/to/my/file.txt"; // Noncompliant {{Refactor your code to get this URI from a customizable parameter.}}
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^
String path_ = "/home/path/to/my/file.txt"; // Noncompliant {{Refactor your code to get this URI from a customizable parameter.}}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why change this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it is not detected anymore otherwise, but I will do it differently don't hesitate to ask a question if you are curious

static String staticRelativePath = "/search"; // Noncompliant

static final String default_uri_path = "/a-great/path/for-this-example"; // Compliant, default_uri is constant and is used in an annotation
String path = "/a-great/path/for-this-example"; // FN, we don't test ot what refer an identifier when collecting them in annotations

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typos

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

static final String relativePath2 = "/group/members";
static final String longRelativePath = "/group/members/list.json"; // Noncompliant
static final String urlPath = "https://www.mywebsite.com"; // Noncompliant
final String finalRelativePath = "/search"; // Noncompliant

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a comment above that it's non-compliant, because static is missing

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

// we use these variables to track when we are visiting an annotation
private final Deque<AnnotationTree> annotationsStack = new ArrayDeque<>();
private final Set<String> variablesUsedInAnnotations = new HashSet<>();
private final ArrayList<VariableTree> hardCodedUri = new ArrayList<>();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's always use interfaces on the left (List instead of ArrayList)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

delete empty line

private final Deque<AnnotationTree> annotationsStack = new ArrayDeque<>();
private final Set<String> variablesUsedInAnnotations = new HashSet<>();

// use semantic ???

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks like a TODO and I don't really understand what it means. Could you try to change the comment please?

return;
}

ModifiersTree modifiers = tree.modifiers();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this could be simplified (or at least the following code passes tests):

    String stringLiteral = stringLiteral(initializer);
    if (stringLiteral == null) {
      return;
    }

    // small relative Uri that are static and final are allowed
    if (ModifiersUtils.hasAll(tree.modifiers(), Modifier.STATIC, Modifier.FINAL)
      && RELATIVE_URI_PATTERN.matcher(stringLiteral).matches()) {
      return;
    }

    if (isHardcodedURI(initializer)) {
      hardCodedUri.add(new HardCodedVariable(tree.simpleName().name(), initializer));
    }

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

.map(IdentifierData::identifier)
.collect(Collectors.toSet());

hardCodedUri.stream()

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Complex expressions are harder to read than simple if-then-else. I played in the IDE and came up with the following:

    for(VariableData v: hardCodedUri) {
      // equals to an identifier with unknown semantic, we cannot compare their symbols
      if(idNamesWithoutSemantic.contains(v.identifier())) {
        continue;
      }

      // idNamesWithSemantic is used to only compare the symbols when their string identifier are the same
      // as comparing symbols is costly
      if(idNamesWithSemantic.contains(v.identifier()) && idSymbols.contains(v.symbol())) {
        continue;
      }

      reportHardcodedURI(v.initializer());
    }

How about changing the code?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@erwan-serandour erwan-serandour enabled auto-merge (squash) December 13, 2024 16:54
@erwan-serandour erwan-serandour merged commit 5f85f13 into master Dec 13, 2024
14 of 15 checks passed
@erwan-serandour erwan-serandour deleted the erwan/S1075 branch December 13, 2024 17:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants