From 76c87a697e1a4c3fa821dbf3065743a2569e56e1 Mon Sep 17 00:00:00 2001 From: erwan-serandour Date: Mon, 16 Dec 2024 15:41:54 +0100 Subject: [PATCH] SONARJAVA-5149 report issues with path delimiters in variable declaration (#4962) --- .../default/src/main/java/checks/HardcodedURICheckSample.java | 1 + .../src/main/java/org/sonar/java/checks/HardcodedURICheck.java | 2 ++ 2 files changed, 3 insertions(+) diff --git a/java-checks-test-sources/default/src/main/java/checks/HardcodedURICheckSample.java b/java-checks-test-sources/default/src/main/java/checks/HardcodedURICheckSample.java index 8ecbcc7419..68c792ff75 100644 --- a/java-checks-test-sources/default/src/main/java/checks/HardcodedURICheckSample.java +++ b/java-checks-test-sources/default/src/main/java/checks/HardcodedURICheckSample.java @@ -35,6 +35,7 @@ void foo(String s, String var) throws URISyntaxException { new File("", s); // Compliant new File("", s + "/" + s); // Noncompliant {{Remove this hard-coded path-delimiter.}} // ^^^ + String path1 = "a" + "/" + "b"; // Noncompliant {{Remove this hard-coded path-delimiter.}} new URI("http:https"); // Compliant new URI("http://www.mywebsite.com"); // Noncompliant {{Refactor your code to get this URI from a customizable parameter.}} diff --git a/java-checks/src/main/java/org/sonar/java/checks/HardcodedURICheck.java b/java-checks/src/main/java/org/sonar/java/checks/HardcodedURICheck.java index 63f18525b9..14d3f2faf3 100644 --- a/java-checks/src/main/java/org/sonar/java/checks/HardcodedURICheck.java +++ b/java-checks/src/main/java/org/sonar/java/checks/HardcodedURICheck.java @@ -179,6 +179,8 @@ private void checkVariable(VariableTree tree) { String stringLiteral = stringLiteral(initializer); if (stringLiteral == null) { + // The check below applies only to binary expressions, so we apply it only if the initializer is not a literal. + reportStringConcatenationWithPathDelimiter(initializer); return; }