dart-code-checker / dart-code-metrics

Software analytics tool that helps developers analyse and improve software quality.
https://dcm.dev
Other
860 stars 265 forks source link

[New rule] Ensure dispose/close called #1229

Closed Ravensof closed 1 year ago

Ravensof commented 1 year ago

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

Additional comments

No response

incendial commented 1 year ago

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

Ravensof commented 1 year ago

@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)

incendial commented 1 year ago

Called where?

incendial commented 1 year ago

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?

Ravensof commented 1 year ago

warning should be appear in places where disposable object were created

image

incendial commented 1 year ago

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.

Ravensof commented 1 year ago

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)

incendial commented 1 year ago

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

Ravensof commented 1 year ago

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?

incendial commented 1 year ago

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

Ravensof commented 1 year ago

okay, thanks again

incendial commented 1 year ago

@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.

Ravensof commented 1 year ago

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

incendial commented 1 year ago

but we cant use teams version,

What stops you from it?

Ravensof commented 1 year ago

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 :[

incendial commented 1 year ago

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?

Ravensof commented 1 year ago

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

Ravensof commented 1 year ago

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 😅