-
ALM used: GitHub
-
CI system used: Jenkins
-
Reproduced in: SonarCloud, SonarLint in Eclipse
-
Languages of the repository: Java
-
Error observed
Even though thefilter
method can return different values, the rule java:S3516 reports:Refactor this method to not always return the same value.
-
Workaround: removal of Lombok log removes the remark
-
Steps to reproduce
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 lombok.extern.log4j.Log4j2;
@Log4j2
public class TestS3516 {
boolean filter(SelectionKey selectionKey, String hostAddress) {
if (hostAddress == null) {
return true;
}
return getInetSocketAddress(selectionKey)
.map(address -> address.getAddress().getHostAddress().equals(hostAddress)).orElse(false);
}
private Optional<InetSocketAddress> getInetSocketAddress(SelectionKey selectionKey) {
SelectableChannel channel = selectionKey.channel();
if (channel instanceof SocketChannel socketChannel) {
try {
return Optional.of(((InetSocketAddress) socketChannel.getRemoteAddress()));
} catch (IOException e) {
log.error("", e);
}
}
return Optional.empty();
}
}