dart-code-checker/dart-code-metrics

[New rule] Ensure dispose/close called

Ravensof opened this issue · 18 comments

Rule details

i want to be sure that my classes canceled/disposed, like Sink`s

What type of rule is this?

Warns about a potential problem

Example code

class CustomClass {
  void disposeFunction()
}

// bad

final custom = CustomClass() // lint, forgot to use disposeFunction

// good

final custom = CustomClass() // no lint
...
custom.disposeFunction()

Participation

  • I am willing to submit a pull request to implement this rule.

Additional comments

No response

@Ravensof can you elaborate on when exactly dispose is supposed to be called? Right now it's not clear at all.

@incendial
sorry, dont know how to say it correct.
i want it works something like lint rule for sinks https://dart-lang.github.io/linter/lints/close_sinks.html

just defines in analysis_options.yaml something like

dart_code_metrics:
  rules:
    close_disposable:
      -  DisposableMixin.dispose

then create class

class CustomClass with DisposableMixin { }

and when i create new object which is DisposableMixin there should be check that dispose ever called for that new object (or for variable where it was created)

Called where?

I mean there are multiple places where dispose can be called. For example, if it's a local variable and it is passed to another function, it can be disposed in two places. So it's not clear where the warning should appear.

Or you mean only class fields?

warning should be appear in places where disposable object were created

image

Got it, the fields.
This rule is available in the Teams version https://dcm.dev/docs/rules/flutter/dispose-fields/. No plans to add to the free version.

i dont know how its better in cases when disposable passed to another function.

in one side you have to dispose object in that level where you create it.
but dont know how to be here:

          Provider<Repository>(
            create: (context) => Repository(),
            dispose: (context, value) => value.dispose(),
          ),

maybe passing to another function should be counted as call dispose (like with lint close_sinks rule)

The case with providers is also covered by another Teams rule, that will become available in few days.

Got it, the fields. This rule is available in the Teams version https://dcm.dev/docs/rules/flutter/dispose-fields/. No plans to add to the free version.

thanks, that was fast, but how to apply it to my class? it just checking method named dispose?

Yes. After the release of 1.4.0, it will also check for 'close' and 'cancel'.

okay, thanks again

@Ravensof and the second part you've asked for https://dcm.dev/docs/rules/provider/dispose-providers/, covers the dispose calls in Provider's dispose.

@incendial thanks, im appreciate it. it could be very useful, but we cant use teams version, i didnt even knew it exists -_-'

but we cant use teams version,

What stops you from it?

but we cant use teams version,

What stops you from it?

because of politics visa/master_card not working in our country.
even it works, for my application (which is hobby) its expensive,
and for job i just cant say to my employeer that we need to buy teams version because of one rule :[

and for job i just cant say to my employeer that we need to buy teams version because of one rule :[

Is it really only one rule?

Is it really only one rule?

we're feeling fine with free version. as i said we didnt knew about teams version and didnt check its benefits

i found some useful rules for myself, but im a regular programmer and i dont feel i have a chance to suggest it for business.
btw thanks for help 😅