linter icon indicating copy to clipboard operation
linter copied to clipboard

A lint for shadowing members?

Open xster opened this issue 8 years ago • 16 comments

As an extension to https://www.dartlang.org/guides/language/effective-dart/usage#dont-use-this-when-not-needed-to-avoid-shadowing

Would it make sense to add a new lint type for (potentially unintentionally) shadowing an inherited member?

class A {
  @protected
  int get stuff => ... blah ...;
}

class B extends A {
  [either] int stuff = ... blah ...;
  
  [1000 lines later]

  void someMethod() {
    [or] int stuff = ... blah ...;
    int thingy = stuff; // I thought I was getting super.stuff
  }
}

xster avatar Apr 10 '17 19:04 xster

IMHO that one just shouldn't be allowed. It's this case I think we need a lint for:

class A {
  @protected
  int get stuff => ... blah ...;
}

class B extends A {
  void someMethod() {
    int stuff;
    // 10 lines later
    int thingy = stuff; // I thought I was getting super.stuff
  }
}

...which happens in particular when you renamed A.stuff and prior to that it didn't get shadowed by the local variable.

Hixie avatar Apr 10 '17 19:04 Hixie

cc @a14n

Hixie avatar Apr 10 '17 19:04 Hixie

I made a rule avoid_shadowing available in https://github.com/a14n/linter/tree/avoid_shadowing branch.

The rule is perhaps a little too aggressive. It adds a lint if there's already a similar name visible (see test).

On Flutter I get 155 lints:

  • https://github.com/flutter/flutter/blob/7b0b5c5760eaa0279782c1fa061b3d7a7acf2ba9/examples/layers/widgets/gestures.dart#L36
  • https://github.com/flutter/flutter/blob/7b0b5c5760eaa0279782c1fa061b3d7a7acf2ba9/examples/layers/widgets/sectors.dart#L34
  • https://github.com/flutter/flutter/blob/7b0b5c5760eaa0279782c1fa061b3d7a7acf2ba9/examples/layers/widgets/custom_render_box.dart#L33
  • https://github.com/flutter/flutter/blob/7b0b5c5760eaa0279782c1fa061b3d7a7acf2ba9/examples/layers/rendering/src/sector_layout.dart#L171
  • https://github.com/flutter/flutter/blob/7b0b5c5760eaa0279782c1fa061b3d7a7acf2ba9/examples/layers/raw/canvas.dart#L25
  • https://github.com/flutter/flutter/blob/7b0b5c5760eaa0279782c1fa061b3d7a7acf2ba9/examples/stocks/lib/stock_arrow.dart#L17
  • https://github.com/flutter/flutter/blob/7b0b5c5760eaa0279782c1fa061b3d7a7acf2ba9/dev/devicelab/lib/framework/adb.dart#L385
  • https://github.com/flutter/flutter/blob/7b0b5c5760eaa0279782c1fa061b3d7a7acf2ba9/dev/tools/dartdoc.dart#L68
  • https://github.com/flutter/flutter/blob/7b0b5c5760eaa0279782c1fa061b3d7a7acf2ba9/dev/manual_tests/material_arc.dart#L233
  • https://github.com/flutter/flutter/blob/7b0b5c5760eaa0279782c1fa061b3d7a7acf2ba9/dev/manual_tests/material_arc.dart#L71
  • https://github.com/flutter/flutter/blob/7b0b5c5760eaa0279782c1fa061b3d7a7acf2ba9/dev/manual_tests/material_arc.dart#L245
  • https://github.com/flutter/flutter/blob/7b0b5c5760eaa0279782c1fa061b3d7a7acf2ba9/dev/manual_tests/material_arc.dart#L58
  • https://github.com/flutter/flutter/blob/7b0b5c5760eaa0279782c1fa061b3d7a7acf2ba9/dev/manual_tests/drag_and_drop.dart#L161
  • https://github.com/flutter/flutter/blob/7b0b5c5760eaa0279782c1fa061b3d7a7acf2ba9/dev/manual_tests/overlay_geometry.dart#L34
  • https://github.com/flutter/flutter/blob/7b0b5c5760eaa0279782c1fa061b3d7a7acf2ba9/packages/flutter_tools/test/devfs_test.dart#L416
  • https://github.com/flutter/flutter/blob/7b0b5c5760eaa0279782c1fa061b3d7a7acf2ba9/packages/flutter_tools/lib/src/application_package.dart#L83
  • https://github.com/flutter/flutter/blob/7b0b5c5760eaa0279782c1fa061b3d7a7acf2ba9/packages/flutter_tools/lib/src/application_package.dart#L112
  • https://github.com/flutter/flutter/blob/7b0b5c5760eaa0279782c1fa061b3d7a7acf2ba9/packages/flutter_tools/lib/src/application_package.dart#L180
  • https://github.com/flutter/flutter/blob/7b0b5c5760eaa0279782c1fa061b3d7a7acf2ba9/packages/flutter_tools/lib/src/application_package.dart#L163
  • https://github.com/flutter/flutter/blob/7b0b5c5760eaa0279782c1fa061b3d7a7acf2ba9/packages/flutter_tools/lib/src/base/version.dart#L54
  • https://github.com/flutter/flutter/blob/7b0b5c5760eaa0279782c1fa061b3d7a7acf2ba9/packages/flutter_tools/lib/src/base/version.dart#L56
  • https://github.com/flutter/flutter/blob/7b0b5c5760eaa0279782c1fa061b3d7a7acf2ba9/packages/flutter_tools/lib/src/base/version.dart#L55
  • https://github.com/flutter/flutter/blob/7b0b5c5760eaa0279782c1fa061b3d7a7acf2ba9/packages/flutter_tools/lib/src/base/version.dart#L67
  • https://github.com/flutter/flutter/blob/7b0b5c5760eaa0279782c1fa061b3d7a7acf2ba9/packages/flutter_tools/lib/src/ios/devices.dart#L118
  • https://github.com/flutter/flutter/blob/7b0b5c5760eaa0279782c1fa061b3d7a7acf2ba9/packages/flutter_tools/lib/src/ios/devices.dart#L101
  • https://github.com/flutter/flutter/blob/7b0b5c5760eaa0279782c1fa061b3d7a7acf2ba9/packages/flutter_tools/lib/src/ios/devices.dart#L108
  • https://github.com/flutter/flutter/blob/7b0b5c5760eaa0279782c1fa061b3d7a7acf2ba9/packages/flutter_tools/lib/src/doctor.dart#L392
  • https://github.com/flutter/flutter/blob/7b0b5c5760eaa0279782c1fa061b3d7a7acf2ba9/packages/flutter_tools/lib/src/doctor.dart#L323
  • https://github.com/flutter/flutter/blob/7b0b5c5760eaa0279782c1fa061b3d7a7acf2ba9/packages/flutter_tools/lib/src/doctor.dart#L399
  • https://github.com/flutter/flutter/blob/7b0b5c5760eaa0279782c1fa061b3d7a7acf2ba9/packages/flutter_tools/lib/src/doctor.dart#L431
  • https://github.com/flutter/flutter/blob/7b0b5c5760eaa0279782c1fa061b3d7a7acf2ba9/packages/flutter_tools/lib/src/doctor.dart#L391
  • https://github.com/flutter/flutter/blob/7b0b5c5760eaa0279782c1fa061b3d7a7acf2ba9/packages/flutter_tools/lib/src/plugins.dart#L22
  • https://github.com/flutter/flutter/blob/7b0b5c5760eaa0279782c1fa061b3d7a7acf2ba9/packages/flutter_tools/lib/src/plugins.dart#L21
  • https://github.com/flutter/flutter/blob/7b0b5c5760eaa0279782c1fa061b3d7a7acf2ba9/packages/flutter_tools/lib/src/commands/create.dart#L114
  • https://github.com/flutter/flutter/blob/7b0b5c5760eaa0279782c1fa061b3d7a7acf2ba9/packages/flutter_tools/lib/src/commands/run.dart#L190
  • https://github.com/flutter/flutter/blob/7b0b5c5760eaa0279782c1fa061b3d7a7acf2ba9/packages/flutter_tools/lib/src/commands/run.dart#L265
  • https://github.com/flutter/flutter/blob/7b0b5c5760eaa0279782c1fa061b3d7a7acf2ba9/packages/flutter_tools/lib/src/commands/fuchsia_reload.dart#L179
  • https://github.com/flutter/flutter/blob/7b0b5c5760eaa0279782c1fa061b3d7a7acf2ba9/packages/flutter_tools/lib/src/commands/fuchsia_reload.dart#L188
  • https://github.com/flutter/flutter/blob/7b0b5c5760eaa0279782c1fa061b3d7a7acf2ba9/packages/flutter_tools/lib/src/android/android_studio.dart#L83
  • https://github.com/flutter/flutter/blob/7b0b5c5760eaa0279782c1fa061b3d7a7acf2ba9/packages/flutter_tools/lib/src/android/android_studio.dart#L283
  • https://github.com/flutter/flutter/blob/7b0b5c5760eaa0279782c1fa061b3d7a7acf2ba9/packages/flutter_tools/lib/src/android/android_studio.dart#L256
  • https://github.com/flutter/flutter/blob/7b0b5c5760eaa0279782c1fa061b3d7a7acf2ba9/packages/flutter_tools/lib/src/android/android_studio.dart#L71
  • https://github.com/flutter/flutter/blob/7b0b5c5760eaa0279782c1fa061b3d7a7acf2ba9/packages/flutter_tools/lib/src/version.dart#L185
  • https://github.com/flutter/flutter/blob/7b0b5c5760eaa0279782c1fa061b3d7a7acf2ba9/packages/flutter_tools/lib/src/vmservice.dart#L255
  • https://github.com/flutter/flutter/blob/7b0b5c5760eaa0279782c1fa061b3d7a7acf2ba9/packages/flutter_tools/lib/src/vmservice.dart#L586
  • https://github.com/flutter/flutter/blob/7b0b5c5760eaa0279782c1fa061b3d7a7acf2ba9/packages/flutter_tools/lib/src/vmservice.dart#L574
  • https://github.com/flutter/flutter/blob/7b0b5c5760eaa0279782c1fa061b3d7a7acf2ba9/packages/flutter_driver/lib/src/find.dart#L191
  • https://github.com/flutter/flutter/blob/7b0b5c5760eaa0279782c1fa061b3d7a7acf2ba9/packages/flutter_driver/lib/src/find.dart#L190
  • https://github.com/flutter/flutter/blob/7b0b5c5760eaa0279782c1fa061b3d7a7acf2ba9/packages/flutter_driver/lib/src/find.dart#L95
  • https://github.com/flutter/flutter/blob/7b0b5c5760eaa0279782c1fa061b3d7a7acf2ba9/packages/flutter_test/lib/src/widget_tester.dart#L307
  • https://github.com/flutter/flutter/blob/7b0b5c5760eaa0279782c1fa061b3d7a7acf2ba9/packages/flutter_test/lib/src/binding.dart#L994
  • https://github.com/flutter/flutter/blob/7b0b5c5760eaa0279782c1fa061b3d7a7acf2ba9/packages/flutter_test/lib/src/controller.dart#L461
  • https://github.com/flutter/flutter/blob/7b0b5c5760eaa0279782c1fa061b3d7a7acf2ba9/packages/flutter_test/lib/src/controller.dart#L471
  • https://github.com/flutter/flutter/blob/7b0b5c5760eaa0279782c1fa061b3d7a7acf2ba9/packages/flutter/test/material/material_test.dart#L44
  • https://github.com/flutter/flutter/blob/7b0b5c5760eaa0279782c1fa061b3d7a7acf2ba9/packages/flutter/lib/src/material/switch.dart#L356
  • https://github.com/flutter/flutter/blob/7b0b5c5760eaa0279782c1fa061b3d7a7acf2ba9/packages/flutter/lib/src/material/progress_indicator.dart#L412
  • https://github.com/flutter/flutter/blob/7b0b5c5760eaa0279782c1fa061b3d7a7acf2ba9/packages/flutter/lib/src/material/progress_indicator.dart#L234
  • https://github.com/flutter/flutter/blob/7b0b5c5760eaa0279782c1fa061b3d7a7acf2ba9/packages/flutter/lib/src/material/progress_indicator.dart#L89
  • https://github.com/flutter/flutter/blob/7b0b5c5760eaa0279782c1fa061b3d7a7acf2ba9/packages/flutter/lib/src/material/input_decorator.dart#L344
  • https://github.com/flutter/flutter/blob/7b0b5c5760eaa0279782c1fa061b3d7a7acf2ba9/packages/flutter/lib/src/material/tabs.dart#L624
  • https://github.com/flutter/flutter/blob/7b0b5c5760eaa0279782c1fa061b3d7a7acf2ba9/packages/flutter/lib/src/material/tabs.dart#L619
  • https://github.com/flutter/flutter/blob/7b0b5c5760eaa0279782c1fa061b3d7a7acf2ba9/packages/flutter/lib/src/material/checkbox.dart#L200
  • https://github.com/flutter/flutter/blob/7b0b5c5760eaa0279782c1fa061b3d7a7acf2ba9/packages/flutter/lib/src/material/checkbox.dart#L183
  • https://github.com/flutter/flutter/blob/7b0b5c5760eaa0279782c1fa061b3d7a7acf2ba9/packages/flutter/lib/src/material/bottom_navigation_bar.dart#L570
  • https://github.com/flutter/flutter/blob/7b0b5c5760eaa0279782c1fa061b3d7a7acf2ba9/packages/flutter/lib/src/material/slider.dart#L483
  • https://github.com/flutter/flutter/blob/7b0b5c5760eaa0279782c1fa061b3d7a7acf2ba9/packages/flutter/lib/src/material/slider.dart#L451
  • https://github.com/flutter/flutter/blob/7b0b5c5760eaa0279782c1fa061b3d7a7acf2ba9/packages/flutter/lib/src/material/time_picker.dart#L90
  • https://github.com/flutter/flutter/blob/7b0b5c5760eaa0279782c1fa061b3d7a7acf2ba9/packages/flutter/lib/src/material/mergeable_material.dart#L677
  • https://github.com/flutter/flutter/blob/7b0b5c5760eaa0279782c1fa061b3d7a7acf2ba9/packages/flutter/lib/src/material/text_selection.dart#L134
  • https://github.com/flutter/flutter/blob/7b0b5c5760eaa0279782c1fa061b3d7a7acf2ba9/packages/flutter/lib/src/material/text_selection.dart#L71
  • https://github.com/flutter/flutter/blob/7b0b5c5760eaa0279782c1fa061b3d7a7acf2ba9/packages/flutter/lib/src/material/radio.dart#L199
  • https://github.com/flutter/flutter/blob/7b0b5c5760eaa0279782c1fa061b3d7a7acf2ba9/packages/flutter/lib/src/widgets/overscroll_indicator.dart#L427
  • https://github.com/flutter/flutter/blob/7b0b5c5760eaa0279782c1fa061b3d7a7acf2ba9/packages/flutter/lib/src/widgets/semantics_debugger.dart#L312
  • https://github.com/flutter/flutter/blob/7b0b5c5760eaa0279782c1fa061b3d7a7acf2ba9/packages/flutter/lib/src/widgets/scroll_physics.dart#L73
  • https://github.com/flutter/flutter/blob/7b0b5c5760eaa0279782c1fa061b3d7a7acf2ba9/packages/flutter/lib/src/widgets/scroll_physics.dart#L131
  • https://github.com/flutter/flutter/blob/7b0b5c5760eaa0279782c1fa061b3d7a7acf2ba9/packages/flutter/lib/src/widgets/basic.dart#L3156
  • https://github.com/flutter/flutter/blob/7b0b5c5760eaa0279782c1fa061b3d7a7acf2ba9/packages/flutter/lib/src/widgets/basic.dart#L2851
  • https://github.com/flutter/flutter/blob/7b0b5c5760eaa0279782c1fa061b3d7a7acf2ba9/packages/flutter/lib/src/widgets/gesture_detector.dart#L400
  • https://github.com/flutter/flutter/blob/7b0b5c5760eaa0279782c1fa061b3d7a7acf2ba9/packages/flutter/lib/src/widgets/overlay.dart#L565
  • https://github.com/flutter/flutter/blob/7b0b5c5760eaa0279782c1fa061b3d7a7acf2ba9/packages/flutter/lib/src/widgets/scroll_position.dart#L223
  • https://github.com/flutter/flutter/blob/7b0b5c5760eaa0279782c1fa061b3d7a7acf2ba9/packages/flutter/lib/src/widgets/heroes.dart#L111
  • https://github.com/flutter/flutter/blob/7b0b5c5760eaa0279782c1fa061b3d7a7acf2ba9/packages/flutter/lib/src/widgets/pages.dart#L33
  • https://github.com/flutter/flutter/blob/7b0b5c5760eaa0279782c1fa061b3d7a7acf2ba9/packages/flutter/lib/src/widgets/framework.dart#L4058
  • https://github.com/flutter/flutter/blob/7b0b5c5760eaa0279782c1fa061b3d7a7acf2ba9/packages/flutter/lib/src/widgets/framework.dart#L4107
  • https://github.com/flutter/flutter/blob/7b0b5c5760eaa0279782c1fa061b3d7a7acf2ba9/packages/flutter/lib/src/widgets/framework.dart#L4114
  • https://github.com/flutter/flutter/blob/7b0b5c5760eaa0279782c1fa061b3d7a7acf2ba9/packages/flutter/lib/src/widgets/framework.dart#L2696
  • https://github.com/flutter/flutter/blob/7b0b5c5760eaa0279782c1fa061b3d7a7acf2ba9/packages/flutter/lib/src/widgets/framework.dart#L4122
  • https://github.com/flutter/flutter/blob/7b0b5c5760eaa0279782c1fa061b3d7a7acf2ba9/packages/flutter/lib/src/widgets/framework.dart#L4071
  • https://github.com/flutter/flutter/blob/7b0b5c5760eaa0279782c1fa061b3d7a7acf2ba9/packages/flutter/lib/src/widgets/layout_builder.dart#L145
  • https://github.com/flutter/flutter/blob/7b0b5c5760eaa0279782c1fa061b3d7a7acf2ba9/packages/flutter/lib/src/widgets/layout_builder.dart#L132
  • https://github.com/flutter/flutter/blob/7b0b5c5760eaa0279782c1fa061b3d7a7acf2ba9/packages/flutter/lib/src/widgets/page_view.dart#L58
  • https://github.com/flutter/flutter/blob/7b0b5c5760eaa0279782c1fa061b3d7a7acf2ba9/packages/flutter/lib/src/widgets/page_view.dart#L72
  • https://github.com/flutter/flutter/blob/7b0b5c5760eaa0279782c1fa061b3d7a7acf2ba9/packages/flutter/lib/src/widgets/page_view.dart#L244
  • https://github.com/flutter/flutter/blob/7b0b5c5760eaa0279782c1fa061b3d7a7acf2ba9/packages/flutter/lib/src/widgets/page_view.dart#L83
  • https://github.com/flutter/flutter/blob/7b0b5c5760eaa0279782c1fa061b3d7a7acf2ba9/packages/flutter/lib/src/widgets/page_view.dart#L188
  • https://github.com/flutter/flutter/blob/7b0b5c5760eaa0279782c1fa061b3d7a7acf2ba9/packages/flutter/lib/src/painting/box_painter.dart#L471
  • https://github.com/flutter/flutter/blob/7b0b5c5760eaa0279782c1fa061b3d7a7acf2ba9/packages/flutter/lib/src/painting/box_painter.dart#L453
  • https://github.com/flutter/flutter/blob/7b0b5c5760eaa0279782c1fa061b3d7a7acf2ba9/packages/flutter/lib/src/painting/box_painter.dart#L1313
  • https://github.com/flutter/flutter/blob/7b0b5c5760eaa0279782c1fa061b3d7a7acf2ba9/packages/flutter/lib/src/painting/box_painter.dart#L1273
  • https://github.com/flutter/flutter/blob/7b0b5c5760eaa0279782c1fa061b3d7a7acf2ba9/packages/flutter/lib/src/painting/box_painter.dart#L369
  • https://github.com/flutter/flutter/blob/7b0b5c5760eaa0279782c1fa061b3d7a7acf2ba9/packages/flutter/lib/src/foundation/assertions.dart#L250
  • https://github.com/flutter/flutter/blob/7b0b5c5760eaa0279782c1fa061b3d7a7acf2ba9/packages/flutter/lib/src/foundation/assertions.dart#L218
  • https://github.com/flutter/flutter/blob/7b0b5c5760eaa0279782c1fa061b3d7a7acf2ba9/packages/flutter/lib/src/rendering/sliver_padding.dart#L135
  • https://github.com/flutter/flutter/blob/7b0b5c5760eaa0279782c1fa061b3d7a7acf2ba9/packages/flutter/lib/src/rendering/sliver_padding.dart#L133
  • https://github.com/flutter/flutter/blob/7b0b5c5760eaa0279782c1fa061b3d7a7acf2ba9/packages/flutter/lib/src/rendering/sliver_padding.dart#L134
  • https://github.com/flutter/flutter/blob/7b0b5c5760eaa0279782c1fa061b3d7a7acf2ba9/packages/flutter/lib/src/rendering/sliver_padding.dart#L132
  • https://github.com/flutter/flutter/blob/7b0b5c5760eaa0279782c1fa061b3d7a7acf2ba9/packages/flutter/lib/src/rendering/viewport.dart#L351
  • https://github.com/flutter/flutter/blob/7b0b5c5760eaa0279782c1fa061b3d7a7acf2ba9/packages/flutter/lib/src/rendering/viewport.dart#L243
  • https://github.com/flutter/flutter/blob/7b0b5c5760eaa0279782c1fa061b3d7a7acf2ba9/packages/flutter/lib/src/rendering/viewport.dart#L250
  • https://github.com/flutter/flutter/blob/7b0b5c5760eaa0279782c1fa061b3d7a7acf2ba9/packages/flutter/lib/src/rendering/viewport.dart#L1111
  • https://github.com/flutter/flutter/blob/7b0b5c5760eaa0279782c1fa061b3d7a7acf2ba9/packages/flutter/lib/src/rendering/editable.dart#L399
  • https://github.com/flutter/flutter/blob/7b0b5c5760eaa0279782c1fa061b3d7a7acf2ba9/packages/flutter/lib/src/rendering/editable.dart#L393
  • https://github.com/flutter/flutter/blob/7b0b5c5760eaa0279782c1fa061b3d7a7acf2ba9/packages/flutter/lib/src/rendering/wrap.dart#L516
  • https://github.com/flutter/flutter/blob/7b0b5c5760eaa0279782c1fa061b3d7a7acf2ba9/packages/flutter/lib/src/rendering/wrap.dart#L232
  • https://github.com/flutter/flutter/blob/7b0b5c5760eaa0279782c1fa061b3d7a7acf2ba9/packages/flutter/lib/src/rendering/wrap.dart#L515
  • https://github.com/flutter/flutter/blob/7b0b5c5760eaa0279782c1fa061b3d7a7acf2ba9/packages/flutter/lib/src/rendering/wrap.dart#L517
  • https://github.com/flutter/flutter/blob/7b0b5c5760eaa0279782c1fa061b3d7a7acf2ba9/packages/flutter/lib/src/rendering/wrap.dart#L264
  • https://github.com/flutter/flutter/blob/7b0b5c5760eaa0279782c1fa061b3d7a7acf2ba9/packages/flutter/lib/src/rendering/wrap.dart#L434
  • https://github.com/flutter/flutter/blob/7b0b5c5760eaa0279782c1fa061b3d7a7acf2ba9/packages/flutter/lib/src/rendering/sliver_fixed_extent_list.dart#L111
  • https://github.com/flutter/flutter/blob/7b0b5c5760eaa0279782c1fa061b3d7a7acf2ba9/packages/flutter/lib/src/rendering/paragraph.dart#L279
  • https://github.com/flutter/flutter/blob/7b0b5c5760eaa0279782c1fa061b3d7a7acf2ba9/packages/flutter/lib/src/rendering/paragraph.dart#L260
  • https://github.com/flutter/flutter/blob/7b0b5c5760eaa0279782c1fa061b3d7a7acf2ba9/packages/flutter/lib/src/rendering/sliver_grid.dart#L488
  • https://github.com/flutter/flutter/blob/7b0b5c5760eaa0279782c1fa061b3d7a7acf2ba9/packages/flutter/lib/src/rendering/sliver_persistent_header.dart#L328
  • https://github.com/flutter/flutter/blob/7b0b5c5760eaa0279782c1fa061b3d7a7acf2ba9/packages/flutter/lib/src/rendering/sliver_persistent_header.dart#L286
  • https://github.com/flutter/flutter/blob/7b0b5c5760eaa0279782c1fa061b3d7a7acf2ba9/packages/flutter/lib/src/rendering/sliver_persistent_header.dart#L238
  • https://github.com/flutter/flutter/blob/7b0b5c5760eaa0279782c1fa061b3d7a7acf2ba9/packages/flutter/lib/src/rendering/sliver_persistent_header.dart#L269
  • https://github.com/flutter/flutter/blob/7b0b5c5760eaa0279782c1fa061b3d7a7acf2ba9/packages/flutter/lib/src/rendering/sliver_persistent_header.dart#L329
  • https://github.com/flutter/flutter/blob/7b0b5c5760eaa0279782c1fa061b3d7a7acf2ba9/packages/flutter/lib/src/rendering/sliver_persistent_header.dart#L206
  • https://github.com/flutter/flutter/blob/7b0b5c5760eaa0279782c1fa061b3d7a7acf2ba9/packages/flutter/lib/src/rendering/box.dart#L451
  • https://github.com/flutter/flutter/blob/7b0b5c5760eaa0279782c1fa061b3d7a7acf2ba9/packages/flutter/lib/src/rendering/box.dart#L1356
  • https://github.com/flutter/flutter/blob/7b0b5c5760eaa0279782c1fa061b3d7a7acf2ba9/packages/flutter/lib/src/rendering/box.dart#L452
  • https://github.com/flutter/flutter/blob/7b0b5c5760eaa0279782c1fa061b3d7a7acf2ba9/packages/flutter/lib/src/rendering/box.dart#L1459
  • https://github.com/flutter/flutter/blob/7b0b5c5760eaa0279782c1fa061b3d7a7acf2ba9/packages/flutter/lib/src/rendering/box.dart#L1923
  • https://github.com/flutter/flutter/blob/7b0b5c5760eaa0279782c1fa061b3d7a7acf2ba9/packages/flutter/lib/src/rendering/box.dart#L1938
  • https://github.com/flutter/flutter/blob/7b0b5c5760eaa0279782c1fa061b3d7a7acf2ba9/packages/flutter/lib/src/rendering/box.dart#L1975
  • https://github.com/flutter/flutter/blob/7b0b5c5760eaa0279782c1fa061b3d7a7acf2ba9/packages/flutter/lib/src/rendering/table.dart#L1248
  • https://github.com/flutter/flutter/blob/7b0b5c5760eaa0279782c1fa061b3d7a7acf2ba9/packages/flutter/lib/src/rendering/table.dart#L1263
  • https://github.com/flutter/flutter/blob/7b0b5c5760eaa0279782c1fa061b3d7a7acf2ba9/packages/flutter/lib/src/rendering/sliver_multi_box_adaptor.dart#L185
  • https://github.com/flutter/flutter/blob/7b0b5c5760eaa0279782c1fa061b3d7a7acf2ba9/packages/flutter/lib/src/rendering/sliver.dart#L1059
  • https://github.com/flutter/flutter/blob/7b0b5c5760eaa0279782c1fa061b3d7a7acf2ba9/packages/flutter/lib/src/rendering/object.dart#L1654
  • https://github.com/flutter/flutter/blob/7b0b5c5760eaa0279782c1fa061b3d7a7acf2ba9/packages/flutter/lib/src/rendering/object.dart#L1780
  • https://github.com/flutter/flutter/blob/7b0b5c5760eaa0279782c1fa061b3d7a7acf2ba9/packages/flutter/lib/src/rendering/object.dart#L2118
  • https://github.com/flutter/flutter/blob/7b0b5c5760eaa0279782c1fa061b3d7a7acf2ba9/packages/flutter/lib/src/rendering/object.dart#L2038
  • https://github.com/flutter/flutter/blob/7b0b5c5760eaa0279782c1fa061b3d7a7acf2ba9/packages/flutter/lib/src/rendering/object.dart#L1404
  • https://github.com/flutter/flutter/blob/7b0b5c5760eaa0279782c1fa061b3d7a7acf2ba9/packages/flutter/lib/src/rendering/object.dart#L2047
  • https://github.com/flutter/flutter/blob/7b0b5c5760eaa0279782c1fa061b3d7a7acf2ba9/packages/flutter/lib/src/rendering/proxy_box.dart#L272
  • https://github.com/flutter/flutter/blob/7b0b5c5760eaa0279782c1fa061b3d7a7acf2ba9/packages/flutter/lib/src/rendering/semantics.dart#L510
  • https://github.com/flutter/flutter/blob/7b0b5c5760eaa0279782c1fa061b3d7a7acf2ba9/packages/flutter/lib/src/rendering/block.dart#L70
  • https://github.com/flutter/flutter/blob/7b0b5c5760eaa0279782c1fa061b3d7a7acf2ba9/packages/flutter/lib/src/rendering/shifted_box.dart#L317
  • https://github.com/flutter/flutter/blob/7b0b5c5760eaa0279782c1fa061b3d7a7acf2ba9/packages/flutter/lib/src/cupertino/activity_indicator.dart#L96
  • https://github.com/flutter/flutter/blob/7b0b5c5760eaa0279782c1fa061b3d7a7acf2ba9/packages/flutter/lib/src/cupertino/switch.dart#L367
  • https://github.com/flutter/flutter/blob/7b0b5c5760eaa0279782c1fa061b3d7a7acf2ba9/packages/flutter/lib/src/cupertino/dialog.dart#L272
  • https://github.com/flutter/flutter/blob/7b0b5c5760eaa0279782c1fa061b3d7a7acf2ba9/packages/flutter/lib/src/cupertino/thumb_painter.dart#L38
  • https://github.com/flutter/flutter/blob/7b0b5c5760eaa0279782c1fa061b3d7a7acf2ba9/packages/flutter/lib/src/cupertino/slider.dart#L310
  • https://github.com/flutter/flutter/blob/7b0b5c5760eaa0279782c1fa061b3d7a7acf2ba9/packages/flutter/lib/src/cupertino/slider.dart#L301

a14n avatar Apr 12 '17 23:04 a14n

Randomly clicked on a few. They all look reasonable. Perhaps for an extra level of leniency initially, we can omit local variables shadowing instance methods. Hopefully in those cases if a function is passed where it's expecting something else, it would crash soon enough anyway.

xster avatar Apr 12 '17 23:04 xster

Yeah, shadowing instance methods is probably ok.

Hixie avatar Apr 12 '17 23:04 Hixie

Actually I clicked on a few more and none of those were methods so maybe that's not helpful.

I wonder what the right heuristic is. Having a local variable called Size that overrides the instance field "size" is fine. Having a local variable "widget" that overrides the instance field "widget" is not... but why not?

Hixie avatar Apr 12 '17 23:04 Hixie

Similarly I had the mixed feeling about the lines linted.

However I can imagine some people happy with this rule as it is today. If I look at Checkstyle in the Java world there's a HiddenFieldCheck that has a similar heuristic (that also applies to the parameter names - I should perhaps add their checkings to the rule)

a14n avatar Apr 13 '17 06:04 a14n

I just ran into a case today where this would have helped during a refactor, +1 to the lint.

My specific case was shadowing an instance field with a local variable on accident.

jakemac53 avatar Apr 18 '17 16:04 jakemac53

@a14n curious if there has been any progress on this?

jakemac53 avatar Jan 19 '18 17:01 jakemac53

@jakemac53 I just opened #872 to add an avoid_shadowing rule. It would be great if you can run this ne rule on your project and see if it is fine or if you see some pattern to customize.

Running it on flutter lints the following lines:

@Hixie @xster does the results look good to you?

a14n avatar Jan 23 '18 08:01 a14n

I just looked at a couple of places and had some half-baked thoughts.

I found several lints for code similar to the following:

final double currentTheta = this.currentTheta;

Seems like a fairly common pattern used to avoid invoking a potentially expensive method multiple times when the author is confident that the result of the invoked method won't change during the invocation of the containing method. Perhaps the lint should allow the shadowing if the local name is initialized by invoking the shadowed name.

final Version version = new Version.parse(versionText);

It's also fairly common (though not necessarily good practice) to given variables names that are the same as the type of the variable. Perhaps the lint should allow this pattern as well.

Finally, I wonder whether users really care about following this rule in test code, or whether this rule should limit itself to production code.

Having a local variable called "size" that overrides the instance field "size" is fine. Having a local variable "widget" that overrides the instance field "widget" is not... but why not?

I can think of a couple of possible reasons, though I won't pretend to know which if any are valid.

First, it's possible that some names, such as "size", "length", and "name", are generic enough and common enough that we just expect them to be used as local variable names even when they are also used as member names. Having that expectation might make it easier for us to deal with the cognitive load from shadowing. On the other hand, a name like "widget", while both generic and common, has a specific enough meaning in this context that perhaps it negates that expectation.

Another possibility is that it has less to do with the name and more to do with the perceived complexity of the type of the variable. Perhaps we deal better with shadowing simple types than we do with shadowing complex types.

bwilkerson avatar Jan 23 '18 17:01 bwilkerson

Perhaps the lint should allow the shadowing if the local name is initialized by invoking the shadowed name.

I think we should allow this.

natebosch avatar Jan 24 '18 06:01 natebosch

With the exclusion pattern var x = this.x; Flutter has the 82 below lints (121 without the pattern).

a14n avatar Jan 24 '18 21:01 a14n

Another pattern that we should allow:

String get someValue {
  final someValue = new StringBuffer();
  ...
  return '$someValue';
}

So we should allow shadowing a property inside the implementation of that property.

natebosch avatar Jan 25 '18 00:01 natebosch

+1 for this feature. It would be useful in certain instances where code has been refactored.

e.g. https://github.com/dart-lang/language/issues/737

krp avatar Dec 12 '19 22:12 krp

Does this include closure variable shadowing? I just found a nasty bug that looks something like this (identifier names have been changed to protect their identities):

void prepareDeliciousMeal(Fridge f) {
  final e = f.getSausage();

  // ... more code ...

  final ingredients = f.everything
      .where((e) => e.goesWellWith.contains(e.id));
  cook(ingredients);
}

If this is not the bug for this, just let me know, I'll create another one.

filiph avatar Jul 01 '20 20:07 filiph