Does New Feature "Intention - Inline Else" Lead To Broken Code?

Answered

One of the new features in the latest EAP is Intention // Inline "else" code branch  https://youtrack.jetbrains.com/issue/IDEA-165428

In the example he goes from

private void streamToClient(ResourceResponse resourceResponse, String filename, InputStream stream, String contentType) throws IOException {
if (null == stream) {
logger.info("Warning - stream is null");
} else {
resourceResponse.setContentType(contentType);
resourceResponse.setProperty(CONTENT_DISPOSITION, "attachment;filename=" + filename);
IOUtils.copy(stream, resourceResponse.getPortletOutputStream());
}
}

to

private void streamToClient(ResourceResponse resourceResponse, String filename, InputStream stream, String contentType) throws IOException {
if (null == stream) {
logger.info("Warning - stream is null");
}
resourceResponse.setContentType(contentType);
resourceResponse.setProperty(CONTENT_DISPOSITION, "attachment;filename=" + filename);
IOUtils.copy(stream, resourceResponse.getPortletOutputStream());
}

In the first block there was a null check.

In the second block it gets ignored/logged and the code continues and potentially causes a NullPointerException.

0
1 comment
Avatar
Permanently deleted user

The intention "Unwrap 'else' block" actually changes the semantics of the code, and it warns about that.

0

Please sign in to leave a comment.