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

[BUG] avoid-passing-async-when-sync-expected #1219

Closed plztrial4me closed 1 year ago

plztrial4me commented 1 year ago

Environment and configuration

DCM version: 5.7.2 Dart SDK version: 2.19.6

Configuration ```yaml dart_code_metrics: metrics: cyclomatic-complexity: 20 number-of-parameters: 4 maximum-nesting-level: 5 metrics-exclude: - test/** rules: - avoid-dynamic - no-avoid-passing-async-when-sync-expected - avoid-redundant-async - avoid-unnecessary-type-assertions - avoid-unnecessary-type-casts - avoid-unrelated-type-assertions - avoid-unused-parameters - avoid-nested-conditional-expressions - newline-before-return - no-boolean-literal-compare - no-empty-block - prefer-trailing-comma - prefer-conditional-expressions - no-equal-then-else - prefer-moving-to-variable - no-prefer-match-file-name ```

What did you do?

abstract class Interface1 {}

abstract class Interface2 {}

class Parent {}

class Child1 extends Parent implements Interface1, Interface2 {}

class Child2 extends Parent implements Interface1 {}

class Handler {
  final Parent handler;
  Handler(this.handler);
}

class Manager {
  final List<Handler> handlers;

  Manager(this.handlers) {
    for (final handler in handlers) {
      final commandHandler = handler.handler;
      if (commandHandler is Interface2) {
        print('Interface2');
      }
    }
  }
}

void main() {
  Child1 child1 = Child1();
  Child2 child2 = Child2();

  List<Handler> handlers = [
    Handler(child1),
    Handler(child2),
  ];

  Manager(handlers);
}

What did you expect to happen?

The result is not always FALSE. Problem 'Avoid unrelated "is" assertion. The result is always "false".' shouldn't occured.

What actually happened?

Problem 'Avoid unrelated "is" assertion. The result is always "false".' occurs in the source code below in class Manager.

if (commandHandler is Interface2) {

Participation

Additional comments

No response

incendial commented 1 year ago

This works as expected.

The problem is that commandHandler is Parent and in the Parent class hierarchy there is no class named Interface2. Only child has it, but commandHandler has no knowledge about that.

Consider adding // ignore: avoid-unrelated-type-assertions to ignore this warning.