compose-lints icon indicating copy to clipboard operation
compose-lints copied to clipboard

Modifier reused - use code flow analysis

Open aasitnikov opened this issue 1 year ago • 2 comments

Improves ModifierReusedDetector

  1. Use DataFlowAnalyzer: It replaces the use of obtainAllModifierNames(), and directly tracks references, not just names. This catches more cases of modifier uses, such as tricky Row(modifier = Modifier.then(modifier))
  2. Report only modifier reference location: Instead of reporting the whole method call, report the specific locations where the modifier is being used. This reduces visual clutter and makes problematic modifier uses more visible.
  3. Code flow analysis: Use CFA to detect if modifier is being used multiple times. This allows to write code that was previously considered incorrect:
@Composable
fun Image(modifier: Modifier = Modifier) {
    if (LocalInspectionMode.current) {
        PreviewImage(modifier)
        return
    }
    AsyncImage(modifier)
}

CFA treats blocks and lambdas like simple blocks of code, and also tracks the control flow of return statements and if/when statements. If control flow graph contains path that has two or more such calls, they will be reported.

aasitnikov avatar Nov 14 '24 18:11 aasitnikov

Thanks for the contribution! Unfortunately we can't verify the commit author(s): Alexander Sitnikov <a***@y***.ru>. One possible solution is to add that email to your GitHub account. Alternatively you can change your commits to another email and force push the change. After getting your commits associated with your GitHub account, sign the Salesforce Inc. Contributor License Agreement and this Pull Request will be revalidated.

salesforce-cla[bot] avatar Nov 14 '24 18:11 salesforce-cla[bot]

Thanks for the contribution! Before we can merge this, we need @aasitnikov to sign the Salesforce Inc. Contributor License Agreement.

salesforce-cla[bot] avatar Nov 14 '24 18:11 salesforce-cla[bot]