-
Notifications
You must be signed in to change notification settings - Fork 687
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
SONARJAVA-4699 FP on S3516 when calling a method using objects from "…
…unknown" packages (#4761)
- Loading branch information
1 parent
0f9d866
commit 896bda7
Showing
5 changed files
with
112 additions
and
6 deletions.
There are no files selected for viewing
92 changes: 92 additions & 0 deletions
92
...ult/src/main/files/non-compiling/symbolicexecution/checks/InvariantReturnCheckSample.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
package symbolicexecution.checks; | ||
|
||
import java.io.IOException; | ||
import java.net.InetSocketAddress; | ||
import java.nio.channels.SelectableChannel; | ||
import java.nio.channels.SelectionKey; | ||
import java.nio.channels.SocketChannel; | ||
import java.util.Optional; | ||
import java.util.concurrent.ThreadLocalRandom; | ||
|
||
|
||
public class InvariantReturnCheckSample { | ||
|
||
class CompliantExample1 { | ||
|
||
private static void confuse() { | ||
methodThatDoesNotExist(); // unresolved symbol | ||
} | ||
|
||
String foo() { | ||
if (ThreadLocalRandom.current().nextInt() > 1) { | ||
return null; | ||
} | ||
confuse(); // Compliant | ||
return "something"; | ||
} | ||
} | ||
|
||
class CompliantExample2 { | ||
|
||
boolean filter(SelectableChannel channel) { | ||
if (channel == null){ | ||
return false; | ||
} | ||
return channel == null ? false : | ||
getSth(channel) // Compliant | ||
.isPresent(); | ||
} | ||
|
||
private Optional<InetSocketAddress> getSth(SelectableChannel channel) { | ||
if (channel instanceof SocketChannel socketChannel) { | ||
try { | ||
return Optional.of(((InetSocketAddress) socketChannel.getRemoteAddress())); | ||
} catch (IOException e) { | ||
log.error("", e); // unresolved symbol | ||
} | ||
} | ||
return Optional.empty(); | ||
} | ||
|
||
} | ||
|
||
class CompliantExample3 { | ||
|
||
boolean filter(SelectableChannel channel) { | ||
if (channel == null){ | ||
return false; | ||
} | ||
return getSth(channel) // Compliant | ||
.isPresent(); | ||
} | ||
|
||
private Optional<String> getSth(SelectableChannel channel) { | ||
if (channel instanceof SocketChannel) { | ||
log.info("Test"); // unresolved symbol | ||
return Optional.of("test"); | ||
} | ||
return Optional.empty(); | ||
} | ||
|
||
} | ||
|
||
class NoncompliantExample { | ||
|
||
boolean filter(SelectableChannel channel) { // Noncompliant [[sc=13;ec=19]] {{Refactor this method to not always return the same value.}} | ||
if (channel == null){ | ||
return false; | ||
} | ||
return true ? false : getSth(null); | ||
} | ||
|
||
private Optional<String> getSth(SelectableChannel channel) { | ||
if (channel instanceof SocketChannel) { | ||
log.info("Test"); // unresolved symbol | ||
return Optional.of("test"); | ||
} | ||
return Optional.empty(); | ||
} | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters