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

fix: use-setstate-synchronously edge cases #1128

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
* feat: support ignoring nesting for [`prefer-conditional-expressions`](https://dartcodemetrics.dev/docs/rules/common/prefer-conditional-expressions).
* fix: ignore Providers for ['avoid-returning-widgets'](https://dartcodemetrics.dev/docs/rules/common/avoid-returning-widgets).
* feat: add [`use-setstate-synchronously`](https://dartcodemetrics.dev/docs/rules/flutter/use-setstate-synchronously).
* fix: correctly invalidate edge cases for [`use-setstate-synchronously`](https://dartcodemetrics.dev/docs/rules/flutter/use-setstate-synchronously)

## 5.3.0

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,9 @@ class _AsyncSetStateVisitor extends RecursiveAstVisitor<void> {
}

node.condition.visitChildren(this);
final oldMounted = mounted;
final newMounted = _extractMountedCheck(node.condition);

mounted = newMounted.or(mounted);

final beforeThen = mounted;
node.thenStatement.visitChildren(this);
final afterThen = mounted;
Expand All @@ -86,8 +85,6 @@ class _AsyncSetStateVisitor extends RecursiveAstVisitor<void> {
mounted = beforeThen != afterThen
? afterThen
: _extractMountedCheck(node.condition, permitAnd: false);
} else {
mounted = oldMounted;
}
}

Expand All @@ -103,7 +100,9 @@ class _AsyncSetStateVisitor extends RecursiveAstVisitor<void> {
mounted = newMounted.or(mounted);
node.body.visitChildren(this);

mounted = _blockDiverges(node.body) ? _tryInvert(newMounted) : oldMounted;
if (_blockDiverges(node.body)) {
mounted = _tryInvert(newMounted).or(oldMounted);
}
}

@override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,23 @@
class _FooState extends State<StatefulWidget> {
void fetchData() async {
Widget build(context) {
return FooWidget(
onChange: (value) async {
setState(() {});
await fetchData();
setState(() {}); // LINT

if (mounted) setState(() {});
},
);
}

void customConfig() async {
await fetch();
foobar(); // LINT
this.foobar(); // LINT
}

void pathologicalCases() async {
setState(() {});

await fetch();
Expand Down Expand Up @@ -62,24 +80,21 @@ class _FooState extends State<StatefulWidget> {
return;
}
setState(() {}); // LINT
}

Widget build(context) {
return FooWidget(
onChange: (value) async {
setState(() {});
await fetchData();
setState(() {}); // LINT
while (!mounted || foo || foo) {
return;
}
setState(() {});

if (mounted) setState(() {});
},
);
}
while (mounted) {
await fetch();
}
setState(() {}); // LINT

void customConfig() async {
await fetch();
foobar(); // LINT
this.foobar(); // LINT
if (mounted) {
await fetch();
}
setState(() {}); // LINT
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ void main() {

RuleTestHelper.verifyIssues(
issues: issues,
startLines: [6, 11, 18, 33, 48, 52, 58, 64, 72],
startColumns: [10, 7, 7, 5, 7, 5, 5, 5, 9],
startLines: [7, 24, 29, 36, 51, 66, 70, 76, 82, 92, 97],
startColumns: [9, 10, 7, 7, 5, 7, 5, 5, 5, 5, 5],
locationTexts: [
'setState',
'setState',
Expand All @@ -38,6 +38,8 @@ void main() {
'setState',
'setState',
'setState',
'setState',
'setState',
],
messages: [
"Avoid calling 'setState' past an await point without checking if the widget is mounted.",
Expand All @@ -49,6 +51,8 @@ void main() {
"Avoid calling 'setState' past an await point without checking if the widget is mounted.",
"Avoid calling 'setState' past an await point without checking if the widget is mounted.",
"Avoid calling 'setState' past an await point without checking if the widget is mounted.",
"Avoid calling 'setState' past an await point without checking if the widget is mounted.",
"Avoid calling 'setState' past an await point without checking if the widget is mounted.",
],
);
});
Expand Down Expand Up @@ -77,12 +81,9 @@ void main() {

RuleTestHelper.verifyIssues(
issues: issues,
startLines: [81, 82],
startLines: [16, 17],
startColumns: [5, 10],
locationTexts: [
'foobar',
'foobar',
],
locationTexts: ['foobar', 'foobar'],
messages: [
"Avoid calling 'foobar' past an await point without checking if the widget is mounted.",
"Avoid calling 'foobar' past an await point without checking if the widget is mounted.",
Expand Down
2 changes: 1 addition & 1 deletion website/docs/rules/flutter/use-setstate-synchronously.mdx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import RuleDetails from '@site/src/components/RuleDetails';

<RuleDetails version="UNRELEASED" severity="warning" hasConfig />
<RuleDetails version="5.4.0" severity="warning" hasConfig />

Warns when [`setState`] is called past an *await point* (also known as asynchronous gap) within a subclass of State.

Expand Down