Skip to content
This repository was archived by the owner on Jul 16, 2023. It is now read-only.

[BUG] unused_code doesn't recognize code used with conditional imports #1083

Closed
lavinov-mercury opened this issue Nov 29, 2022 · 3 comments
Closed
Assignees
Labels
area-unused-code type: bug Something isn't working
Milestone

Comments

@lavinov-mercury
Copy link

  • Dart code metrics version: 5.0.1
  • Dart sdk version: 2.18.5

Please show your full configuration:

Default configuration, i've just added code metrics to the project

What did you do? Please include the source code example causing the issue.

Here is a sample project with reproducible issue. I've split implementation for Web & IO, using conditional import:

import 'client.dart'
    if (dart.library.html) 'client_web.dart'
    if (dart.library.io) 'client_io.dart';

and used fabric method like this

SomeClient createClient() => throw Exception('Stub factory');

to create specific client for each env. However, if i run check-unused-code, it fails, considering these fabrics aren't used

code_metrics_conditional_import % flutter pub run dart_code_metrics:metrics check-unused-code lib
✔ Analysis is completed. Preparing the results: 6.4s

lib/client_io.dart:
    ⚠ unused function createClient
      at /Users/lavinov/Development/Flutter/code_metrics_conditional_import/lib/client_io.dart:4:1

lib/client_web.dart:
    ⚠ unused function createClient
      at /Users/lavinov/Development/Flutter/code_metrics_conditional_import/lib/client_web.dart:5:1

✖ total unused code (classes, functions, variables, extensions, enums, mixins and type aliases) - 2
pub finished with exit code 1

What did you expect to happen?

These methods should be considered as used

What actually happened?

Error above

Are you willing to submit a pull request to fix this bug?

No, currently it easily can be worked around using // ignore: unused-code

@incendial
Copy link
Member

incendial commented Dec 1, 2022

@lavinov-mercury thanks for the report and the example! Unexpected to see this problem, coz we have a separate test case for it

if (dart.library.io) 'conditional_file.dart' as config;
but I guess it differs somehow, I'll check

@incendial incendial added this to the 5.1.0 milestone Dec 1, 2022
@incendial
Copy link
Member

incendial commented Dec 1, 2022

Okay, I see, the prefix matters. That's interesting...

@incendial incendial removed this from the 5.1.0 milestone Dec 1, 2022
@incendial incendial added waiting for release Will be available after new version is released and removed in progress labels Dec 8, 2022
@incendial incendial added this to the 5.2.0 milestone Dec 8, 2022
@incendial
Copy link
Member

Fixed in 5.2.0 🚀

@incendial incendial removed the waiting for release Will be available after new version is released label Dec 8, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-unused-code type: bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants