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

False positive on rule UnnecessaryAbstractClass #727

Closed
xgouchet opened this issue Feb 6, 2018 · 6 comments · Fixed by #2513
Closed

False positive on rule UnnecessaryAbstractClass #727

xgouchet opened this issue Feb 6, 2018 · 6 comments · Fixed by #2513

Comments

@xgouchet
Copy link

xgouchet commented Feb 6, 2018

Take for instance the following classes :

abstract class Foo {
    abstract fun bar() : Int

    fun baz() {
    }
}
abstract class Oof : Foo() {
    fun spam() {
    }
}

The class Oof is reported as being an UnnecessaryAbstractClass, because it only contains concrete methods. But in fact, because it inherits from Foo, it also have an abstract, unimplemented method (bar).

@schalkms
Copy link
Member

schalkms commented Feb 6, 2018

Thanks for using detekt!
Detekt does not have type resolution (yet). For now I think this rule should ignore classes which derive from an abstract class. What do you think? @arturbosch @Mauin

@arturbosch
Copy link
Member

Yep, it makes sense for now.
Will you take care of the fix @schalkms ?
Maybe we should open an issue and collect all the improvements to the rules which can be done after type resolution?

@schalkms
Copy link
Member

schalkms commented Feb 6, 2018

I can do that. I think the type-resolution tag is sufficient.

@vanniktech
Copy link
Contributor

Got something similar that should not be flagged:

abstract class Screenshots {
  @get:Rule val demoModeRule = DemoModeRule()

  @Before fun setUp() {
    Screengrab.setDefaultScreenshotStrategy(UiAutomatorScreenshotStrategy())

    tearDown()
  }

  @After fun tearDown() {
    nukeSharedPreferences()
  }

  fun screenshot(name: String) {
    Screengrab.screenshot(name, Screengrab.getDefaultScreenshotStrategy(), ConstantFileWritingScreenshotCallback(application()))
  }

  fun isUsingFastlane() = InstrumentationRegistry.getArguments().getString("isUsingFastlane")?.toBoolean() == true

  private class ConstantFileWritingScreenshotCallback(applicationContext: Context) : FileWritingScreenshotCallback(applicationContext) {
    override fun getScreenshotFile(screenshotDirectory: File, screenshotName: String) =
      File(screenshotDirectory, "$screenshotName.png")
  }
}

There are multiple reasons:

  • using JUnit annotations that can be used downstream in the classes
  • one can still use the provided methods (screenshot & isUsingFastlane)

@schalkms
Copy link
Member

@vanniktech Hmm, I'm a bit skeptical and want to raise some questions. Should detekt really include framework specific things (JUnit in your case)? I'm a bit worried that this leads to implementing different edge cases which are only relevant for a minor audience.

@vanniktech
Copy link
Contributor

I guess it should but I'm biased!

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