Skip to content

feat(bloc_lint): add avoid_returning_existing_instance_from_bloc_provider_create - #4865

Open
dylanpulver wants to merge 1 commit into
felangel:masterfrom
dylanpulver:feat/lint-avoid-returning-existing-bloc-instance
Open

dylanpulver wants to merge 1 commit into
felangel:masterfrom
dylanpulver:feat/lint-avoid-returning-existing-bloc-instance

Conversation

@dylanpulver

@dylanpulver dylanpulver commented Aug 20, 2026 •

Copy link
Copy Markdown

Status

READY

Breaking Changes

NO

Description

Adds the avoid_returning_existing_instance_from_bloc_provider_create lint rule requested in #4462. It warns when a BlocProvider's create callback returns an instance that already exists, which matters because the default constructor makes the provider the owner and closes what create returned when it leaves the tree.

Fixes #4462

It looks at BlocProvider(...) and BlocProvider<T>(...), at a top-level create argument whose value is a function literal, checking the arrow body and every top-level return in a block body. An expression counts as existing when it is a plain reference with no invocation (bloc, _bloc, this.bloc, widget.bloc, widget.bloc!), or a lookup that can only hand back something already built: context.read<T>() / context.watch<T>() where the receiver is context or the create parameter's name, and BlocProvider.of<T>(context).

The obvious alternative, reporting anything that is not a constructor invocation, misfires because bloc_lint runs on the parser without type information. create: (_) => createCounterBloc(), create: (_) => getIt<CounterBloc>() from a locator registered as a factory, and create: (_) => CounterBloc.initial() all return fresh instances and cannot be told apart from a cached one by tokens alone. So the rule reports only shapes that cannot construct anything, and stays quiet on shapes that might.

Two consequences. A getter that constructs on every read is still reported, which seems acceptable since a reader cannot tell what that line does at the call site either. And a cascade such as create: (_) => bloc..add(Increment()) is not reported today, a miss I would rather add later than guess at now.

Registered in allRules, exported from bloc_lint.dart and rules.dart, added to all.yaml, listed in the README, and documented under docs/src/content/docs/lint-rules. I left it out of recommended.yaml since that set is your call.

18 lintTest cases, 9 linting and 9 not. Linted: field reference, widget.bloc, no trailing comma, generic BlocProvider<T>, create preceded by other arguments, context.read, watch on a renamed callback parameter, BlocProvider.of, and a block body with a return. Not linted: inline construction, const constructor, named constructor, factory function, service locator, block body returning a new instance, create tear-off, BlocProvider.value, and a RepositoryProvider.

dart test passes at 169 tests in packages/bloc_lint. format_coverage reports 100.0% line coverage, 639/639, including 65/65 in the new rule file. dart format . reports nothing; dart analyze --fatal-infos --fatal-warnings . is clean apart from two unrecognized-lint-name warnings in the shared analysis_options.yaml that also appear on master with my local SDK. The docs site builds and npm run format:check passes.

Type of Change

  • ✨ New feature (non-breaking change which adds functionality)
  • 🛠️ Bug fix (non-breaking change which fixes an issue)
  • ❌ Breaking change (fix or feature that would cause existing functionality to change)
  • 🧹 Code refactor
  • ✅ Build configuration change
  • 📝 Documentation
  • 🗑️ Chore

@dylanpulver
dylanpulver force-pushed the feat/lint-avoid-returning-existing-bloc-instance branch from 34151b0 to e8b0992 Compare November 8, 2026 06:56

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feat(bloc_lint): avoid_returning_existing_instance_from_bloc_provider_create

1 participant