Use dart analyze package for num.clamp (#139867)
Extacted from #130101, dropped the `@_debugAssert` stuff from that PR so it's easier to review.
This commit is contained in:
@@ -19,3 +19,33 @@ class Foo {
|
||||
|
||||
/// Simply avoid this
|
||||
/// and simply do that.
|
||||
|
||||
class ClassWithAClampMethod {
|
||||
ClassWithAClampMethod clamp(double min, double max) => this;
|
||||
}
|
||||
|
||||
void testNoDoubleClamp(int input) {
|
||||
final ClassWithAClampMethod nonDoubleClamp = ClassWithAClampMethod();
|
||||
// ignore: unnecessary_nullable_for_final_variable_declarations
|
||||
final ClassWithAClampMethod? nonDoubleClamp2 = nonDoubleClamp;
|
||||
// ignore: unnecessary_nullable_for_final_variable_declarations
|
||||
final int? nullableInt = input;
|
||||
final double? nullableDouble = nullableInt?.toDouble();
|
||||
|
||||
nonDoubleClamp.clamp(0, 2);
|
||||
input.clamp(0, 2);
|
||||
input.clamp(0.0, 2); // bad.
|
||||
input.toDouble().clamp(0, 2); // bad.
|
||||
|
||||
nonDoubleClamp2?.clamp(0, 2);
|
||||
nullableInt?.clamp(0, 2);
|
||||
nullableInt?.clamp(0, 2.0); // bad
|
||||
nullableDouble?.clamp(0, 2); // bad.
|
||||
|
||||
// ignore: unused_local_variable
|
||||
final ClassWithAClampMethod Function(double, double)? tearOff1 = nonDoubleClamp2?.clamp;
|
||||
// ignore: unused_local_variable
|
||||
final num Function(num, num)? tearOff2 = nullableInt?.clamp; // bad.
|
||||
// ignore: unused_local_variable
|
||||
final num Function(num, num)? tearOff3 = nullableDouble?.clamp; // bad.
|
||||
}
|
||||
|
||||
@@ -7,6 +7,8 @@ import 'dart:io';
|
||||
import 'package:path/path.dart' as path;
|
||||
|
||||
import '../analyze.dart';
|
||||
import '../custom_rules/analyze.dart';
|
||||
import '../custom_rules/no_double_clamp.dart';
|
||||
import '../utils.dart';
|
||||
import 'common.dart';
|
||||
|
||||
@@ -226,4 +228,28 @@ void main() {
|
||||
expect(result, contains(':20'));
|
||||
expect(result, contains(':21'));
|
||||
});
|
||||
|
||||
test('analyze.dart - clampDouble', () async {
|
||||
final String result = await capture(() => analyzeFrameworkWithRules(
|
||||
testRootPath,
|
||||
<AnalyzeRule>[noDoubleClamp],
|
||||
), shouldHaveErrors: true);
|
||||
final String lines = <String>[
|
||||
'║ packages/flutter/lib/bar.dart:37: input.clamp(0.0, 2)',
|
||||
'║ packages/flutter/lib/bar.dart:38: input.toDouble().clamp(0, 2)',
|
||||
'║ packages/flutter/lib/bar.dart:42: nullableInt?.clamp(0, 2.0)',
|
||||
'║ packages/flutter/lib/bar.dart:43: nullableDouble?.clamp(0, 2)',
|
||||
'║ packages/flutter/lib/bar.dart:48: nullableInt?.clamp',
|
||||
'║ packages/flutter/lib/bar.dart:50: nullableDouble?.clamp',
|
||||
]
|
||||
.map((String line) => line.replaceAll('/', Platform.isWindows ? r'\' : '/'))
|
||||
.join('\n');
|
||||
expect(result,
|
||||
'╔═╡ERROR╞═══════════════════════════════════════════════════════════════════════\n'
|
||||
'$lines\n'
|
||||
'║ \n'
|
||||
'║ For performance reasons, we use a custom "clampDouble" function instead of using "double.clamp".\n'
|
||||
'╚═══════════════════════════════════════════════════════════════════════════════\n'
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user