Detekt: Rule to detect undesired method calls

Created on 30 Aug 2018  路  4Comments  路  Source: detekt/detekt

There are certain kinds of method calls that you don't want to see in a production code base, maybe most prominently kotlin.io.println. It would be great to have a rule that detekts (see what I did there? :blush:) these.

feature help wanted needs type and symbol solving rules

Most helpful comment

Just my two cents to this issue (I created a new oen because I couldn't find this so I'm cut&pasting what I wrote there.


ForbiddenMethod

Expected Behavior of the rule

This new rule will allow you to add methods that are forbidden.

Kotlin provides you @Deprecated to force you to use another method. And this annotation should be prefered over ForbiddenMethod but if you are using an external libraries you can't deprecate those methods.

The idea is to use something similar than forbidden imports BUT for methods. The configuration would be something like:

  ForbiddenMethod:
    active: true
    methods: 'com.bumptech.glide.Glide.with(Context), com.bumptech.glide.Glide.with(Activity), io.reactivex.disposables.CompositeDisposable.dispose()'


Context

In out team we are changing or way of work. Some of these changes are like: "from now on, we are not going to use the method XXX". The last one was: from now on we are going to inject the Glide RequestManager so call Glide.with(XXX) is forbidden.

I know that I can create my custom rules but I think that this rule is generic enought to be added in the project. It's exactly the same as ForbiddenImport but when you don't want to forbid all the class, just some methods.

All 4 comments

That's a good idea for a contributed rule. I like it.
Please note, that detekt currently does not support type and symbol solving.

Just my two cents to this issue (I created a new oen because I couldn't find this so I'm cut&pasting what I wrote there.


ForbiddenMethod

Expected Behavior of the rule

This new rule will allow you to add methods that are forbidden.

Kotlin provides you @Deprecated to force you to use another method. And this annotation should be prefered over ForbiddenMethod but if you are using an external libraries you can't deprecate those methods.

The idea is to use something similar than forbidden imports BUT for methods. The configuration would be something like:

  ForbiddenMethod:
    active: true
    methods: 'com.bumptech.glide.Glide.with(Context), com.bumptech.glide.Glide.with(Activity), io.reactivex.disposables.CompositeDisposable.dispose()'


Context

In out team we are changing or way of work. Some of these changes are like: "from now on, we are not going to use the method XXX". The last one was: from now on we are going to inject the Glide RequestManager so call Glide.with(XXX) is forbidden.

I know that I can create my custom rules but I think that this rule is generic enought to be added in the project. It's exactly the same as ForbiddenImport but when you don't want to forbid all the class, just some methods.

@BraisGabin thanks for contributing your ideas. Sounds good to me!
Do you have the resources to take a stab at this rule?

Do you have the resources to take a stab at this rule?

I do have a prototype for this rule here #1954

Was this page helpful?
0 / 5 - 0 ratings