Dart-Code / Dart-Code

Dart and Flutter support for VS Code
https://dartcode.org/
MIT License
1.46k stars 294 forks source link

False positive null warning #5092

Closed larssn closed 1 week ago

larssn commented 2 weeks ago

Describe the bug I've run into a weird false positive regarding null safety.

A few pictures speak more than a thousand words:

Picture 1 Picture 2

So while I simply have PunchCardItem? item; I'm notified that it cannot be null, even though it's clearly nullable. In picture 2, I've simply added = null; to the variable, which fixes the problem.

To Reproduce I've created an example which can be pasted into dartpad.dev, which illustrates the issue:

void main() {
  Map<String, PunchCardItem>? punchCardItems;

  String productId = 'abc';
  PunchCardItem? item;
    for (final pc in (punchCardItems ?? {}).values) {
      if (pc.id == productId && (item == null || item.price > pc.price)) {}
    }
}

class PunchCardItem {
  String id = 'abc';
  num price = 0;
}
Workspace Environment ```text Dart Code extension: 3.88.0 Flutter extension: 3.88.0 (activated) App: Visual Studio Code App Host: desktop Version: mac 1.89.0 Workspace type: Flutter (LSP) Dart (3.3.4): /Users/lars/fvm/versions/stable/bin/cache/dart-sdk Flutter (3.19.6): /Users/lars/fvm/versions/stable (sdk gphone64 arm64 (android-arm64/android)) ```
Output from 'dart info' `/Users/lars/fvm/versions/stable/bin/cache/dart-sdk/bin/dart info` If providing this information as part of reporting a bug, please review the information below to ensure it only contains things you're comfortable posting publicly. #### General info - Dart 3.3.4 (stable) (Tue Apr 16 19:56:12 2024 +0000) on "macos_arm64" - on macos / Version 14.4.1 (Build 23E224) - locale is en-US #### Project info - sdk constraint: '>=3.3.3 <4.0.0' - dependencies: android_id, audioplayers, bloc, built_collection, built_value, cached_network_image, cloud_firestore, cloud_firestore_platform_interface, collection, country_pickers, crypto, dart_date, decimal, device_info_plus, expansion_widget, file_picker, firebase_analytics, firebase_auth, firebase_core, firebase_crashlytics, firebase_storage, firebase_ui_firestore, flutter, flutter_bloc, flutter_dotenv, flutter_html, flutter_keyboard_visibility, flutter_localizations, flutter_material_color_picker, flutter_nfc_kit, flutter_riverpod, flutter_slidable, flutter_timezone, geography, html_unescape, http, image, image_picker, in_app_update, internet_connection_checker, intl, meta, package_info_plus, path_provider, phone_numbers_parser, pointycastle, positioned_tap_detector_2, qr_flutter, random_string, rational, reorderables, retry, rxdart, shared_preferences, sprintf, time, timezone, url_launcher, uuid, validators, xml - dev_dependencies: build_runner, built_value_generator, flutter_lints - elided dependencies: 13 #### Process info | Memory | CPU | Elapsed time | Command line | | ------: | ---: | -----------: | ------------------------------------------------------------------------------------------ | | 26 MB | 0.0% | 01:56:15 | dart devtools --no-launch-browser | | 1507 MB | 0.0% | 01-09:25:53 | dart language-server --protocol=lsp --client-id=VS-Code --client-version=3.88.0 | | 55 MB | 0.0% | 01-09:25:53 | flutter_tools.snapshot daemon | | 208 MB | 0.0% | 01:57:18 | flutter_tools.snapshot run --machine --start-paused -d emulator-5554 --target lib/main.dart |
Output from 'flutter doctor' `/Users/lars/fvm/versions/stable/bin/flutter doctor -v` ```text [✓] Flutter (Channel stable, 3.19.6, on macOS 14.4.1 23E224 darwin-arm64, locale en-US) • Flutter version 3.19.6 on channel stable at /Users/lars/fvm/versions/stable • Upstream repository https://github.com/flutter/flutter.git • Framework revision 54e66469a9 (2 weeks ago), 2024-04-17 13:08:03 -0700 • Engine revision c4cd48e186 • Dart version 3.3.4 • DevTools version 2.31.1 [✓] Android toolchain - develop for Android devices (Android SDK version 34.0.0) • Android SDK at /Users/lars/Library/Android/sdk • Platform android-34, build-tools 34.0.0 • ANDROID_HOME = /Users/lars/Library/Android/sdk • Java binary at: /Applications/Android Studio.app/Contents/jbr/Contents/Home/bin/java • Java version OpenJDK Runtime Environment (build 17.0.10+0-17.0.10b1087.21-11572160) • All Android licenses accepted. [!] Xcode - develop for iOS and macOS (Xcode 15.3) • Xcode at /Applications/Xcode.app/Contents/Developer • Build 15E204a ✗ Unable to get list of installed Simulator runtimes. • CocoaPods version 1.15.2 [✓] Chrome - develop for the web • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome [✓] Android Studio (version 2023.3) • Android Studio at /Applications/Android Studio.app/Contents • Flutter plugin can be installed from: 🔨 https://plugins.jetbrains.com/plugin/9212-flutter • Dart plugin can be installed from: 🔨 https://plugins.jetbrains.com/plugin/6351-dart • Java version OpenJDK Runtime Environment (build 17.0.10+0-17.0.10b1087.21-11572160) [✓] VS Code (version 1.89.0) • VS Code at /Applications/Visual Studio Code.app/Contents • Flutter extension version 3.88.0 [✓] Connected device (4 available) • sdk gphone64 arm64 (mobile) • emulator-5554 • android-arm64 • Android 13 (API 33) (emulator) • Another Phone (mobile) • 00008110-000404840C7A801E • ios • iOS 17.4.1 21E236 • macOS (desktop) • macos • darwin-arm64 • macOS 14.4.1 23E224 darwin-arm64 • Chrome (web) • chrome • web-javascript • Google Chrome 124.0.6367.119 [✓] Network resources • All expected network resources are available. ! Doctor found issues in 1 category. ```
Zekfad commented 1 week ago

Message is have incorrect wording, but it's not a bug, it should say that value is always null, therefore condition is always true, since you don't assign the variable and it has null by default.

I think this should go to dart-lang/sdk as analyzer issue.

DanTup commented 1 week ago

Yep, I agree with @Zekfad's conclusion - the wording is wrong but being a warning is correct.

Please file at https://github.com/dart-lang/sdk as this will need resolving there. Thanks!

DanTup commented 1 week ago

Looks like there's an existing issue about this here:

https://github.com/dart-lang/sdk/issues/53670