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

CanBeNonNullable false positive #5331

Closed
TWiStErRob opened this issue Sep 21, 2022 · 5 comments · Fixed by #5332
Closed

CanBeNonNullable false positive #5331

TWiStErRob opened this issue Sep 21, 2022 · 5 comments · Fixed by #5332
Labels

Comments

@TWiStErRob
Copy link
Member

TWiStErRob commented Sep 21, 2022

Steps to Reproduce

import javax.xml.stream.XMLStreamWriter

inline fun XMLStreamWriter.attribute(name: String, value: Any) {
	writeAttribute(name, value.toString())
}

inline fun <T : Any> XMLStreamWriter.optionalAttribute(
	name: String,
	value: T?,
	transformation: (T) -> String
) {
	value?.let { attribute(name, transformation(it)) }
}

Expected Behavior

No flagging.

Observed Behavior

XMLStreamWriterDSL.kt:80:70: The nullable parameter 'value' can be made non-nullable. [CanBeNonNullable]

Context

The whole point of this method is to hide the nullability in a named nice fun. The method does the null check, so that it can be just called, and the lambda doesn't have to deal with a null-check. It allows this change:
image
which actually fixes NestedScopeFunctions.

I'm not entirely sure this is a false positive, I could be mis-interpreting what CanBeNonNullable does.

Your Environment

  • Version of detekt used: 1.21.0
  • Version of Gradle used (if applicable): 7.5.1
  • Operating System and version: Windows
  • Link to your project (if it's a public repository): code is not live yet.
@TWiStErRob TWiStErRob added the bug label Sep 21, 2022
@TWiStErRob TWiStErRob changed the title CanBeNullable false positive CanBeNonNullable false positive Sep 21, 2022
@BraisGabin
Copy link
Member

This is an expected behavior. The function does nothing when the value is null so it should not allow null values in the first place.

If you really want to keep the function signature I think that a @Suppress is the way to go.

@TWiStErRob
Copy link
Member Author

Thanks. If I make it non-null then I can remove the method, but then I need to put the null check outside of every usage.
Should I be doing this? (I'm trying to find a general way to handle this, because it's quite a recurring pattern)

if(documentation != null)
   attribute("documentation", documentation.toAsciiString())

@TWiStErRob
Copy link
Member Author

(feel free to move this to a discussion)

@BraisGabin
Copy link
Member

Yes, that's the idea, to move the check up instead of inside the function. Don't have functions that do "nothing".

But as I said, if that have sense for your use case you can always @Suppress the issue in your case. I mean, the parameter could be non-nullable But even it "could" you prefer to keep it nullable.

@TWiStErRob
Copy link
Member Author

TWiStErRob commented Sep 22, 2022

@BraisGabin added your explanation to the rule: #5332

(We follow the same pattern at work, btw, ifs outside.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants