kotlin:S6516: Functional interface implementations should use lambda expressions

Please provide

  • Operating system: MacOS
  • SonarLint plugin version: 9.0.0.75308
  • Programming language you’re coding in: Kotlin
  • Is connected mode used: No

We are getting trouble complying with this lint.

view.viewTreeObserver
          .addOnGlobalLayoutListener(object : OnGlobalLayoutListener {
              override fun onGlobalLayout() {
                  // Remove this listener to avoid memory leak
                  adView.viewTreeObserver.removeOnGlobalLayoutListener(this)
                  // Add padding to avoid banner ad overlapping RecyclerView
                  recyclerView.setPadding(recyclerOrigLeftPadding, 0, 0, adsContainer.height)
              }
          })

The lint is triggering kotlin:S6516, which can be solve by changing the code to this

view.viewTreeObserver
          .addOnGlobalLayoutListener  {
                  // Remove this listener to avoid memory leak
                  adView.viewTreeObserver.removeOnGlobalLayoutListener(this)
                  // Add padding to avoid banner ad overlapping RecyclerView
                  recyclerView.setPadding(recyclerOrigLeftPadding, 0, 0, adsContainer.height)
              }

However by doing so, the this parameter is no longer valid and will not compile as it can no longer get the instance of this single function interface.

1 Like

Hi Bitwise DEVS,

Thanks for reporting! I have checked and can confirm that this is an issue in our Kotlin analyzer. There should be an exception from this rule when the this argument is used in the function, because lambda expressions have no own this.

I created an issue here.

Cheers,

Marco

1 Like