Skip to content

Add lint rule for debug-only RenderObject getters#183706

Closed
futpib wants to merge 4 commits into
flutter:masterfrom
futpib:lint-avoid-debug-only-getters
Closed

Add lint rule for debug-only RenderObject getters#183706
futpib wants to merge 4 commits into
flutter:masterfrom
futpib:lint-avoid-debug-only-getters

Conversation

@futpib

@futpib futpib commented Mar 15, 2026

Copy link
Copy Markdown

Adds a custom analyze rule that warns when debug-only RenderObject getters (debugNeedsLayout, debugNeedsPaint, debugNeedsCompositedLayerUpdate, debugNeedsSemanticsUpdate) are used outside of assert statements.

These getters are only meaningful in debug mode — in release builds they silently return false. Using them in non-assert code can lead to subtle bugs where the app behaves differently in debug vs release mode.

Legitimate non-assert usages (e.g. in WidgetInspectorService, which only runs in debug/profile mode) can be suppressed with a // flutter_ignore: debug_only_rendering_getter (see analyze.dart) comment.

Follow-up to #183697, fixes #183696.

Pre-launch Checklist

@github-actions github-actions Bot added the framework flutter/packages/flutter repository. See also f: labels. label Mar 15, 2026

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces a new lint rule to prevent the incorrect use of debug-only RenderObject getters outside of assert statements. The implementation of the new rule is solid, and it includes corresponding tests and updates to existing code to suppress legitimate usages. My feedback includes a suggestion to make the type checking in the new lint rule more robust by verifying the library URI of RenderObject in addition to its name.

Comment thread dev/bots/custom_rules/avoid_debug_only_rendering_getters.dart
@justinmc justinmc requested review from Piinks and justinmc March 24, 2026 22:06
@Piinks Piinks added the CICD Run CI/CD label Apr 7, 2026

@Piinks Piinks left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for sending a PR!

Comment thread dev/bots/custom_rules/avoid_debug_only_rendering_getters.dart Outdated
@Piinks Piinks force-pushed the lint-avoid-debug-only-getters branch from 9184015 to fbb08bf Compare April 7, 2026 20:35
@github-actions github-actions Bot removed the CICD Run CI/CD label Apr 7, 2026
@Piinks Piinks added the CICD Run CI/CD label Apr 7, 2026
@Piinks

Piinks commented Apr 7, 2026

Copy link
Copy Markdown
Contributor

@futpib it looks like this does not currently compile, can you take a look?

@justinmc justinmc marked this pull request as draft April 7, 2026 22:17
@flutter-dashboard

Copy link
Copy Markdown

This pull request has been changed to a draft. The currently pending flutter-gold status will not be able to resolve until a new commit is pushed or the change is marked ready for review again.

For more guidance, visit Writing a golden file test for package:flutter.

Reviewers: Read the Tree Hygiene page and make sure this patch meets those guidelines before LGTMing.

@github-actions github-actions Bot removed the CICD Run CI/CD label Apr 7, 2026
@futpib futpib force-pushed the lint-avoid-debug-only-getters branch from a82127b to 5db02fb Compare April 8, 2026 00:15
@futpib futpib marked this pull request as ready for review April 8, 2026 00:15

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces a new analyzer rule, avoidDebugOnlyRenderingGetters, which prevents the use of specific RenderObject debug getters outside of assert statements or debug-only contexts. The changes include the rule implementation, a refactor of the hasInlineIgnore utility to accept raw content and line information, and new test fixtures. Feedback was provided regarding the strict library URI validation in the rule, which may prevent it from correctly identifying violations within the test environment's mock objects.

Comment thread dev/bots/custom_rules/avoid_debug_only_rendering_getters.dart Outdated
@futpib futpib force-pushed the lint-avoid-debug-only-getters branch from c1cece6 to 1d45ce9 Compare April 8, 2026 03:39
futpib and others added 4 commits April 10, 2026 11:52
…serts

debugNeedsLayout, debugNeedsPaint, debugNeedsCompositedLayerUpdate, and
debugNeedsSemanticsUpdate are only meaningful in debug mode. This custom
analyze rule warns when they are used outside of assert statements,
helping prevent subtle bugs where these getters silently return false
in release builds.

Legitimate non-assert usages (e.g. in WidgetInspectorService, which
only runs in debug/profile mode) can be suppressed with a
flutter_ignore comment directive.
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
- Replace custom _hasTrailingFlutterIgnore with shared hasInlineIgnore
  from utils.dart, as suggested in review
- Fix compilation error: library.source.uri → library.uri (source
  getter doesn't exist on LibraryElement in analyzer 10.1.0)
- Generalize hasInlineIgnore to accept String content + LineInfo
  instead of ParseStringResult, so it works with ResolvedUnitResult too
The test infrastructure uses fake RenderObject classes without package
config, so library URIs won't match package:flutter/... — consistent
with how render_box_intrinsics.dart checks by name only.
@futpib futpib force-pushed the lint-avoid-debug-only-getters branch from 1d45ce9 to acd7ca6 Compare April 10, 2026 11:52
@futpib

futpib commented Apr 10, 2026

Copy link
Copy Markdown
Author

@Piinks I think it's fixed now as far as I can see without ci

@futpib futpib requested a review from Piinks April 10, 2026 14:09

@chunhtai chunhtai left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will be hesitate about enforcing this in framework mainly for cases that debug widget like widget inspector or error widget has valid use case to call these debug method, but I think this is something that is suitable lint for flutter_lint that is used for flutter app #185091

@Piinks Piinks left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this have captured

if (box.debugNeedsLayout) {
?

Looking at #184627, I found we are pretty inconsistent about calling these methods or not outside of debug mode in the framework, I wonder if this lint is too prohibitive for those cases where we already do call on these methods.

Also, what about debugDoingLayout, debugDoingPaint, debugDoingThisResize, debugDoingThisLayout, and debugCanParentUseSize?

Chatting with @chunhtai, they filed #185091, we might need to approach this more comprehensively to address the inconsistencies we have in the framework.

}

/// Returns true if [enclosingElement] is `RenderObject` or a subclass of it.
static bool _isRenderObjectSubclass(InterfaceElement enclosingElement) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The _isRenderObjectSubclass check currently relies on the class name RenderObject. While sufficient for the Flutter repo's internal analysis, it could be made more robust by verifying the library URI to avoid false positives with unrelated classes that happen to be named RenderObject.

@Piinks

Piinks commented Apr 15, 2026

Copy link
Copy Markdown
Contributor

I will be hesitate about enforcing this in framework mainly for cases that debug widget like widget inspector or error widget has valid use case to call these debug method, but I think this is something that is suitable lint for flutter_lint that is used for flutter app #185091

Making this a flutter_lint is a great idea @chunhtai!

@futpib futpib closed this Apr 28, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

framework flutter/packages/flutter repository. See also f: labels.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Proposal: debugNeedsPaint, debugNeedsLayout, debugNeedsCompositedLayerUpdate crash in release mode instead of returning gracefully

3 participants