openrewrite / openrewrite/rewrite

Missing `@Nullable` annotations on visitor methods

Open
#3,321 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
Java
Stars
3.7k
Forks
570
Avg merge
13h 12m
Merged PRs (30d)
261

Description

What version of OpenRewrite are you using?

I am using

  • OpenRewrite v7.40.3

How are you running OpenRewrite?

N/A

What is the smallest, simplest way to reproduce the problem?

In a kotlin recipe repo:

class MyVisitor : JavaVisitor<ExecutionContext>() {
    override fun visitMethodInvocation(method: J.MethodInvocation, p: ExecutionContext): J? = null
}
class MyIsoVisitor : JavaIsoVisitor<ExecutionContext>() {
    override fun visitMethodInvocation(method: J.MethodInvocation, p: ExecutionContext): J.MethodInvocation? = null
}

What did you expect to see?

warning-free code, as (to my understanding) pretty much any visitXyz method can "legally" return null if that element should be removed

What did you see instead?

In IDEA 2023.1.2, the override keyword is yellow-squiggle-underlined with this tooltip:

Override 'fun visitMethodInvocation(method: J.MethodInvocation, p: ExecutionContext): J?' has incorrect nullability in its signature comparing with overridden 'fun visitMethodInvocation(method: J.MethodInvocation, p: ExecutionContext): J'This warning will become an error soon. See https://youtrack.jetbrains.com/issue/KT-36770 for details

In the short term, this warning can be suppressed, but if it indeed becomes an error case in a future Kotlin version, then this will cause headaches.

Are you interested in contributing a fix to OpenRewrite?

50/50 on this one; I could mindlessly add the @Nullable annotation to a whole bunch of visitXyz methods (across Java(Iso)Visitor and all (?) other base visitors), but if there are any cases where it's not actually nullable, then I'd likely make a mistake. And I'm not sure how much effort will cascade from this, to handle any newly-created nullability warnings for usages of these methods within the library

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the JavaVisitor and JavaIsoVisitor visitMethodInvocation methods shown in the reproduction, then inspect the other base visitor methods for their nullability contracts and existing annotation usage. Check how these methods are called within the library and verify that Kotlin overrides no longer produce nullability warnings without introducing incorrect contracts.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, kotlin
Domain
tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.