Repository navigation
feat(bloc_lint): add avoid_returning_existing_instance_from_bloc_provider_create - #4865
Open
dylanpulver wants to merge 1 commit into
Open
dylanpulver wants to merge 1 commit into
dylanpulver wants to merge 1 commit into
Conversation
dylanpulver
force-pushed
the
feat/lint-avoid-returning-existing-bloc-instance
branch
from
November 8, 2026 06:56
34151b0 to
e8b0992
Compare
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Status
READY
Breaking Changes
NO
Description
Adds the
avoid_returning_existing_instance_from_bloc_provider_createlint rule requested in #4462. It warns when aBlocProvider'screatecallback returns an instance that already exists, which matters because the default constructor makes the provider the owner and closes whatcreatereturned when it leaves the tree.Fixes #4462
It looks at
BlocProvider(...)andBlocProvider<T>(...), at a top-levelcreateargument whose value is a function literal, checking the arrow body and every top-levelreturnin 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 iscontextor thecreateparameter's name, andBlocProvider.of<T>(context).The obvious alternative, reporting anything that is not a constructor invocation, misfires because
bloc_lintruns on the parser without type information.create: (_) => createCounterBloc(),create: (_) => getIt<CounterBloc>()from a locator registered as a factory, andcreate: (_) => 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 frombloc_lint.dartandrules.dart, added toall.yaml, listed in the README, and documented underdocs/src/content/docs/lint-rules. I left it out ofrecommended.yamlsince that set is your call.18
lintTestcases, 9 linting and 9 not. Linted: field reference,widget.bloc, no trailing comma, genericBlocProvider<T>,createpreceded by other arguments,context.read,watchon a renamed callback parameter,BlocProvider.of, and a block body with areturn. Not linted: inline construction,constconstructor, named constructor, factory function, service locator, block body returning a new instance,createtear-off,BlocProvider.value, and aRepositoryProvider.dart testpasses at 169 tests inpackages/bloc_lint.format_coveragereports 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 sharedanalysis_options.yamlthat also appear on master with my local SDK. The docs site builds andnpm run format:checkpasses.Type of Change