Skip to content

Commit b98cd1f

Browse files
[pigeon] Enable warnings as errors for remaining languages (#3317)
[pigeon] Enable warnings as errors for remaining languages
1 parent 475cf82 commit b98cd1f

File tree

36 files changed

+127
-155
lines changed

36 files changed

+127
-155
lines changed

packages/pigeon/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
## 9.0.3
2+
3+
* [kotlin] Fixes compiler warnings in generated output.
4+
* [swift] Fixes compiler warnings in generated output.
5+
16
## 9.0.2
27

38
* [swift] Removes safe casting from decode process.

packages/pigeon/lib/generator_tools.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import 'ast.dart';
1111
/// The current version of pigeon.
1212
///
1313
/// This must match the version in pubspec.yaml.
14-
const String pigeonVersion = '9.0.2';
14+
const String pigeonVersion = '9.0.3';
1515

1616
/// Read all the content from [stdin] to a String.
1717
String readStdin() {

packages/pigeon/lib/java_generator.dart

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -458,7 +458,7 @@ class JavaGenerator extends StructuredGenerator<JavaOptions> {
458458
'$returnType $output = channelReply == null ? null : ((Number) channelReply).longValue();');
459459
} else {
460460
indent.writeln(
461-
'$returnType $output = ($returnType) channelReply;');
461+
'$returnType $output = ${_cast('channelReply', javaType: returnType)};');
462462
}
463463
indent.writeln('callback.reply($output);');
464464
});
@@ -627,8 +627,8 @@ class JavaGenerator extends StructuredGenerator<JavaOptions> {
627627
String accessor = 'args.get($index)';
628628
if (isEnum(root, arg.type)) {
629629
accessor = _intToEnum(accessor, arg.type.baseName);
630-
} else if (argType != 'Object') {
631-
accessor = '($argType) $accessor';
630+
} else {
631+
accessor = _cast(accessor, javaType: argType);
632632
}
633633
indent.writeln('$argType $argName = $accessor;');
634634
if (!arg.type.isNullable) {
@@ -646,7 +646,7 @@ class JavaGenerator extends StructuredGenerator<JavaOptions> {
646646
method.returnType.isVoid ? 'null' : 'result';
647647
const String resultName = 'resultCallback';
648648
indent.format('''
649-
Result<$returnType> $resultName =
649+
Result<$returnType> $resultName =
650650
\t\tnew Result<$returnType>() {
651651
\t\t\tpublic void success($returnType result) {
652652
\t\t\t\twrapped.add(0, $resultValue);
@@ -871,6 +871,13 @@ String _nullsafeJavaTypeForDartType(TypeDeclaration type) {
871871
return '$nullSafe${_javaTypeForDartType(type)}';
872872
}
873873

874+
/// Returns an expression to cast [variable] to [javaType].
875+
String _cast(String variable, {required String javaType}) {
876+
// Special-case Object, since casting to Object doesn't do anything, and
877+
// causes a warning.
878+
return javaType == 'Object' ? variable : '($javaType) $variable';
879+
}
880+
874881
/// Casts variable named [varName] to the correct host datatype for [field].
875882
/// This is for use in codecs where we may have a map representation of an
876883
/// object.
@@ -884,6 +891,6 @@ String _castObject(
884891
classes.map((Class x) => x.name).contains(field.type.baseName)) {
885892
return '($varName == null) ? null : ${hostDatatype.datatype}.fromList((ArrayList<Object>) $varName)';
886893
} else {
887-
return '(${hostDatatype.datatype}) $varName';
894+
return _cast(varName, javaType: hostDatatype.datatype);
888895
}
889896
}

packages/pigeon/lib/kotlin_generator.dart

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -180,12 +180,13 @@ class KotlinGenerator extends StructuredGenerator<KotlinOptions> {
180180
final HostDatatype hostDatatype = _getHostDatatype(root, field);
181181
String toWriteValue = '';
182182
final String fieldName = field.name;
183+
final String safeCall = field.type.isNullable ? '?' : '';
183184
if (!hostDatatype.isBuiltin &&
184185
customClassNames.contains(field.type.baseName)) {
185-
toWriteValue = '$fieldName?.toList()';
186+
toWriteValue = '$fieldName$safeCall.toList()';
186187
} else if (!hostDatatype.isBuiltin &&
187188
customEnumNames.contains(field.type.baseName)) {
188-
toWriteValue = '$fieldName?.raw';
189+
toWriteValue = '$fieldName$safeCall.raw';
189190
} else {
190191
toWriteValue = fieldName;
191192
}
@@ -244,7 +245,8 @@ class KotlinGenerator extends StructuredGenerator<KotlinOptions> {
244245
indent.addln(
245246
'.let { if (it is Int) it.toLong() else it as Long? }');
246247
} else {
247-
indent.writeln('val ${field.name} = $listValue as $fieldType?');
248+
indent.writeln(
249+
'val ${field.name} = ${_cast(listValue, kotlinType: '$fieldType?')}');
248250
}
249251
} else {
250252
if (!hostDatatype.isBuiltin &&
@@ -260,7 +262,8 @@ class KotlinGenerator extends StructuredGenerator<KotlinOptions> {
260262
indent
261263
.addln('.let { if (it is Int) it.toLong() else it as Long }');
262264
} else {
263-
indent.writeln('val ${field.name} = $listValue as $fieldType');
265+
indent.writeln(
266+
'val ${field.name} = ${_cast(listValue, kotlinType: fieldType)}');
264267
}
265268
}
266269
});
@@ -380,13 +383,15 @@ class KotlinGenerator extends StructuredGenerator<KotlinOptions> {
380383
indent.writeln('callback()');
381384
});
382385
} else {
383-
final String forceUnwrap = func.returnType.isNullable ? '?' : '';
384386
indent.addScoped('{', '}', () {
385387
if (func.returnType.baseName == 'int') {
388+
final String forceUnwrap =
389+
func.returnType.isNullable ? '?' : '';
386390
indent.writeln(
387391
'val result = if (it is Int) it.toLong() else it as$forceUnwrap Long');
388392
} else {
389-
indent.writeln('val result = it as$forceUnwrap $returnType');
393+
indent.writeln(
394+
'val result = ${_cast('it', kotlinType: returnType, safeCast: func.returnType.isNullable)}');
390395
}
391396
indent.writeln('callback(result)');
392397
});
@@ -507,7 +512,6 @@ class KotlinGenerator extends StructuredGenerator<KotlinOptions> {
507512

508513
indent.write('channel.setMessageHandler ');
509514
indent.addScoped('{ $messageVarName, reply ->', '}', () {
510-
indent.writeln('var wrapped = listOf<Any?>()');
511515
final List<String> methodArguments = <String>[];
512516
if (method.arguments.isNotEmpty) {
513517
indent.writeln('val args = message as List<Any?>');
@@ -543,6 +547,7 @@ class KotlinGenerator extends StructuredGenerator<KotlinOptions> {
543547
});
544548
});
545549
} else {
550+
indent.writeln('var wrapped: List<Any?>');
546551
indent.write('try ');
547552
indent.addScoped('{', '}', () {
548553
if (method.returnType.isVoid) {
@@ -669,15 +674,15 @@ String _castForceUnwrap(String value, TypeDeclaration type, Root root) {
669674
type.isNullable ? '$value == null ? null : ' : '';
670675
return '$nullableConditionPrefix${_kotlinTypeForDartType(type)}.ofRaw($value as Int)$forceUnwrap';
671676
} else {
672-
final String castUnwrap = type.isNullable ? '?' : '';
673-
674677
// The StandardMessageCodec can give us [Integer, Long] for
675678
// a Dart 'int'. To keep things simple we just use 64bit
676679
// longs in Pigeon with Kotlin.
677680
if (type.baseName == 'int') {
681+
final String castUnwrap = type.isNullable ? '?' : '';
678682
return '$value.let { if (it is Int) it.toLong() else it as$castUnwrap Long }';
679683
} else {
680-
return '$value as$castUnwrap ${_kotlinTypeForDartType(type)}';
684+
return _cast(value,
685+
kotlinType: _kotlinTypeForDartType(type), safeCast: type.isNullable);
681686
}
682687
}
683688
}
@@ -741,3 +746,13 @@ String _nullsafeKotlinTypeForDartType(TypeDeclaration type) {
741746
final String nullSafe = type.isNullable ? '?' : '';
742747
return '${_kotlinTypeForDartType(type)}$nullSafe';
743748
}
749+
750+
/// Returns an expression to cast [variable] to [kotlinType].
751+
String _cast(String variable,
752+
{required String kotlinType, bool safeCast = false}) {
753+
// Special-case Any, since no-op casts cause warnings.
754+
if (kotlinType == 'Any?' || (safeCast && kotlinType == 'Any')) {
755+
return variable;
756+
}
757+
return '$variable as${safeCast ? '?' : ''} $kotlinType';
758+
}

packages/pigeon/mock_handler_tester/test/message.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// Use of this source code is governed by a BSD-style license that can be
33
// found in the LICENSE file.
44
//
5-
// Autogenerated from Pigeon (v9.0.2), do not edit directly.
5+
// Autogenerated from Pigeon (v9.0.3), do not edit directly.
66
// See also: https://pub.dev/packages/pigeon
77
// ignore_for_file: public_member_api_docs, non_constant_identifier_names, avoid_as, unused_import, unnecessary_parenthesis, prefer_null_aware_operators, omit_local_variable_types, unused_shown_name, unnecessary_import
88

packages/pigeon/mock_handler_tester/test/test.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// Use of this source code is governed by a BSD-style license that can be
33
// found in the LICENSE file.
44
//
5-
// Autogenerated from Pigeon (v9.0.2), do not edit directly.
5+
// Autogenerated from Pigeon (v9.0.3), do not edit directly.
66
// See also: https://pub.dev/packages/pigeon
77
// ignore_for_file: public_member_api_docs, non_constant_identifier_names, avoid_as, unused_import, unnecessary_parenthesis, unnecessary_import
88
// ignore_for_file: avoid_relative_lib_imports

packages/pigeon/pigeons/android_unittests.dart

Lines changed: 0 additions & 30 deletions
This file was deleted.

packages/pigeon/platform_tests/alternate_language_test_plugin/android/build.gradle

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,7 @@ android {
5050
testImplementation "org.mockito:mockito-core:5.+"
5151
}
5252
}
53+
54+
project.getTasks().withType(JavaCompile){
55+
options.compilerArgs.addAll(["-Xlint:all", "-Werror"])
56+
}

packages/pigeon/platform_tests/alternate_language_test_plugin/android/src/main/java/com/example/alternate_language_test_plugin/CoreTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// Use of this source code is governed by a BSD-style license that can be
33
// found in the LICENSE file.
44
//
5-
// Autogenerated from Pigeon (v9.0.2), do not edit directly.
5+
// Autogenerated from Pigeon (v9.0.3), do not edit directly.
66
// See also: https://pub.dev/packages/pigeon
77

88
package com.example.alternate_language_test_plugin;
@@ -3239,7 +3239,7 @@ public void throwError(Reply<Object> callback) {
32393239
null,
32403240
channelReply -> {
32413241
@SuppressWarnings("ConstantConditions")
3242-
Object output = (Object) channelReply;
3242+
Object output = channelReply;
32433243
callback.reply(output);
32443244
});
32453245
}

packages/pigeon/platform_tests/alternate_language_test_plugin/android/src/test/java/com/example/alternate_language_test_plugin/AllDatatypesTest.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ public void nullValues() {
2626
ByteBuffer message = invocation.getArgument(1);
2727
BinaryMessenger.BinaryReply reply = invocation.getArgument(2);
2828
message.position(0);
29+
@SuppressWarnings("unchecked")
2930
ArrayList<Object> args =
3031
(ArrayList<Object>) FlutterIntegrationCoreApi.getCodec().decodeMessage(message);
3132
ByteBuffer replyData =
@@ -101,6 +102,7 @@ public void hasValues() {
101102
ByteBuffer message = invocation.getArgument(1);
102103
BinaryMessenger.BinaryReply reply = invocation.getArgument(2);
103104
message.position(0);
105+
@SuppressWarnings("unchecked")
104106
ArrayList<Object> args =
105107
(ArrayList<Object>) FlutterIntegrationCoreApi.getCodec().decodeMessage(message);
106108
ByteBuffer replyData =

packages/pigeon/platform_tests/alternate_language_test_plugin/android/src/test/java/com/example/alternate_language_test_plugin/AsyncTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public void asyncSuccess() {
6161
(bytes) -> {
6262
bytes.rewind();
6363
@SuppressWarnings("unchecked")
64-
ArrayList wrapped = (ArrayList) codec.decodeMessage(bytes);
64+
ArrayList<Object> wrapped = (ArrayList<Object>) codec.decodeMessage(bytes);
6565
assertTrue(wrapped.size() == 1);
6666
didCall[0] = true;
6767
});
@@ -88,7 +88,7 @@ public void asyncError() {
8888
(bytes) -> {
8989
bytes.rewind();
9090
@SuppressWarnings("unchecked")
91-
ArrayList wrapped = (ArrayList) codec.decodeMessage(bytes);
91+
ArrayList<Object> wrapped = (ArrayList<Object>) codec.decodeMessage(bytes);
9292
assertTrue(wrapped.size() > 1);
9393
assertEquals("java.lang.Exception: error", (String) wrapped.get(0));
9494
didCall[0] = true;

packages/pigeon/platform_tests/alternate_language_test_plugin/android/src/test/java/com/example/alternate_language_test_plugin/ListTest.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@ public void listInList() {
2828
ByteBuffer message = invocation.getArgument(1);
2929
BinaryMessenger.BinaryReply reply = invocation.getArgument(2);
3030
message.position(0);
31-
ArrayList args = (ArrayList) FlutterSmallApi.getCodec().decodeMessage(message);
31+
@SuppressWarnings("unchecked")
32+
ArrayList<Object> args =
33+
(ArrayList<Object>) FlutterSmallApi.getCodec().decodeMessage(message);
3234
ByteBuffer replyData = FlutterSmallApi.getCodec().encodeMessage(args.get(0));
3335
replyData.position(0);
3436
reply.reply(replyData);

packages/pigeon/platform_tests/alternate_language_test_plugin/android/src/test/java/com/example/alternate_language_test_plugin/MultipleArityTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ public void subtract() {
2222
ByteBuffer message = invocation.getArgument(1);
2323
BinaryMessenger.BinaryReply reply = invocation.getArgument(2);
2424
message.position(0);
25+
@SuppressWarnings("unchecked")
2526
ArrayList<Object> args =
2627
(ArrayList<Object>) MultipleArityFlutterApi.getCodec().decodeMessage(message);
2728
Long arg0 = (Long) args.get(0);

packages/pigeon/platform_tests/alternate_language_test_plugin/android/src/test/java/com/example/alternate_language_test_plugin/NullableReturnsTest.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public void nullArgHostApi() {
3939
(bytes) -> {
4040
bytes.rewind();
4141
@SuppressWarnings("unchecked")
42-
ArrayList wrapped = (ArrayList) codec.decodeMessage(bytes);
42+
ArrayList<Object> wrapped = (ArrayList<Object>) codec.decodeMessage(bytes);
4343
assertTrue(wrapped.size() == 1);
4444
});
4545
}
@@ -52,8 +52,9 @@ public void nullArgFlutterApi() {
5252
ByteBuffer message = invocation.getArgument(1);
5353
BinaryMessenger.BinaryReply reply = invocation.getArgument(2);
5454
message.position(0);
55-
ArrayList args =
56-
(ArrayList)
55+
@SuppressWarnings("unchecked")
56+
ArrayList<Object> args =
57+
(ArrayList<Object>)
5758
NullableReturns.NullableArgFlutterApi.getCodec().decodeMessage(message);
5859
assertNull(args.get(0));
5960
ByteBuffer replyData =

packages/pigeon/platform_tests/alternate_language_test_plugin/android/src/test/java/com/example/alternate_language_test_plugin/PigeonTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public void errorMessage() {
4646
(bytes) -> {
4747
bytes.rewind();
4848
@SuppressWarnings("unchecked")
49-
ArrayList error = (ArrayList) codec.decodeMessage(bytes);
49+
ArrayList<Object> error = (ArrayList<Object>) codec.decodeMessage(bytes);
5050
assertNotNull(error.get(0));
5151
assertNotNull(error.get(1));
5252
String details = (String) error.get(2);

packages/pigeon/platform_tests/alternate_language_test_plugin/android/src/test/java/com/example/alternate_language_test_plugin/PrimitiveTest.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@ private static BinaryMessenger makeMockBinaryMessenger() {
2828
ByteBuffer message = invocation.getArgument(1);
2929
BinaryMessenger.BinaryReply reply = invocation.getArgument(2);
3030
message.position(0);
31-
ArrayList args = (ArrayList) PrimitiveFlutterApi.getCodec().decodeMessage(message);
31+
@SuppressWarnings("unchecked")
32+
ArrayList<Object> args =
33+
(ArrayList<Object>) PrimitiveFlutterApi.getCodec().decodeMessage(message);
3234
Object arg = args.get(0);
3335
if (arg instanceof Long) {
3436
Long longArg = (Long) arg;
@@ -88,7 +90,7 @@ public void primitiveIntHostApi() {
8890
.setMessageHandler(eq("dev.flutter.pigeon.PrimitiveHostApi.anInt"), handler.capture());
8991
MessageCodec<Object> codec = PrimitiveHostApi.getCodec();
9092
@SuppressWarnings("unchecked")
91-
ByteBuffer message = codec.encodeMessage(new ArrayList(Arrays.asList((Integer) 1)));
93+
ByteBuffer message = codec.encodeMessage(new ArrayList<Object>(Arrays.asList((Integer) 1)));
9294
message.rewind();
9395
handler
9496
.getValue()
@@ -97,7 +99,7 @@ public void primitiveIntHostApi() {
9799
(bytes) -> {
98100
bytes.rewind();
99101
@SuppressWarnings("unchecked")
100-
ArrayList wrapped = (ArrayList) codec.decodeMessage(bytes);
102+
ArrayList<Object> wrapped = (ArrayList<Object>) codec.decodeMessage(bytes);
101103
assertTrue(wrapped.size() > 0);
102104
assertEquals(1L, ((Long) wrapped.get(0)).longValue());
103105
});

packages/pigeon/platform_tests/alternate_language_test_plugin/example/ios/Podfile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,5 +38,9 @@ end
3838
post_install do |installer|
3939
installer.pods_project.targets.each do |target|
4040
flutter_additional_ios_build_settings(target)
41+
42+
target.build_configurations.each do |config|
43+
config.build_settings['GCC_TREAT_WARNINGS_AS_ERRORS'] = 'YES'
44+
end
4145
end
4246
end

packages/pigeon/platform_tests/alternate_language_test_plugin/ios/Classes/CoreTests.gen.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// Use of this source code is governed by a BSD-style license that can be
33
// found in the LICENSE file.
44
//
5-
// Autogenerated from Pigeon (v9.0.2), do not edit directly.
5+
// Autogenerated from Pigeon (v9.0.3), do not edit directly.
66
// See also: https://pub.dev/packages/pigeon
77

88
#import <Foundation/Foundation.h>

packages/pigeon/platform_tests/alternate_language_test_plugin/ios/Classes/CoreTests.gen.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// Use of this source code is governed by a BSD-style license that can be
33
// found in the LICENSE file.
44
//
5-
// Autogenerated from Pigeon (v9.0.2), do not edit directly.
5+
// Autogenerated from Pigeon (v9.0.3), do not edit directly.
66
// See also: https://pub.dev/packages/pigeon
77

88
#import "CoreTests.gen.h"

packages/pigeon/platform_tests/flutter_null_safe_unit_tests/lib/core_tests.gen.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// Use of this source code is governed by a BSD-style license that can be
33
// found in the LICENSE file.
44
//
5-
// Autogenerated from Pigeon (v9.0.2), do not edit directly.
5+
// Autogenerated from Pigeon (v9.0.3), do not edit directly.
66
// See also: https://pub.dev/packages/pigeon
77
// ignore_for_file: public_member_api_docs, non_constant_identifier_names, avoid_as, unused_import, unnecessary_parenthesis, prefer_null_aware_operators, omit_local_variable_types, unused_shown_name, unnecessary_import
88

0 commit comments

Comments
 (0)