Skip to content

Commit 1366144

Browse files
eeriipull[bot]
authored andcommitted
clippy: Fix remaining warnings in generated code (servo#31844)
* clippy: fix warnings in generated code * clippy: fix wrap_panic closure warnings
1 parent 485bbeb commit 1366144

File tree

4 files changed

+48
-32
lines changed

4 files changed

+48
-32
lines changed

components/script/dom/bindings/codegen/CodegenRust.py

+33-20
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,11 @@
4040
)
4141

4242
AUTOGENERATED_WARNING_COMMENT = "/* THIS FILE IS AUTOGENERATED - DO NOT EDIT */\n\n"
43-
IGNORED_WARNING_LIST = ['non_camel_case_types', 'non_upper_case_globals', 'unused_imports',
43+
ALLOWED_WARNING_LIST = ['non_camel_case_types', 'non_upper_case_globals', 'unused_imports',
4444
'unused_variables', 'unused_assignments', 'unused_mut',
4545
'clippy::approx_constant', 'clippy::let_unit_value', 'clippy::needless_return',
46-
'clippy::too_many_arguments', 'clippy::unnecessary_cast']
47-
IGNORED_WARNINGS = f"#![allow({','.join(IGNORED_WARNING_LIST)})]\n\n"
46+
'clippy::too_many_arguments', 'clippy::unnecessary_cast', 'clippy::upper_case_acronyms']
47+
ALLOWED_WARNINGS = f"#![allow({','.join(ALLOWED_WARNING_LIST)})]\n\n"
4848

4949
FINALIZE_HOOK_NAME = '_finalize'
5050
TRACE_HOOK_NAME = '_trace'
@@ -1402,14 +1402,16 @@ def __init__(self, argument, index, args, argc, descriptorProvider,
14021402
innerConverter.append(CGGeneric("%s.push(slot);" % arg))
14031403
inner = CGIndenter(CGList(innerConverter, "\n"), 8).define()
14041404

1405+
sub = "" if index == 0 else "- %s" % index
1406+
14051407
self.converter = CGGeneric("""\
14061408
%(init)s;
14071409
if %(argc)s > %(index)s {
1408-
%(arg)s.reserve(%(argc)s as usize - %(index)s);
1410+
%(arg)s.reserve(%(argc)s as usize%(sub)s);
14091411
for variadicArg in %(index)s..%(argc)s {
14101412
%(inner)s
14111413
}
1412-
}""" % {'arg': arg, 'argc': argc, 'index': index, 'inner': inner, 'init': init})
1414+
}""" % {'arg': arg, 'argc': argc, 'index': index, 'inner': inner, 'init': init, 'sub': sub})
14131415

14141416
def define(self):
14151417
return self.converter.define()
@@ -2564,8 +2566,11 @@ def __len__(self):
25642566

25652567
class CGIfElseWrapper(CGList):
25662568
def __init__(self, condition, ifTrue, ifFalse):
2567-
kids = [CGIfWrapper(condition, ifTrue),
2568-
CGWrapper(CGIndenter(ifFalse), pre=" else {\n", post="\n}")]
2569+
if ifFalse.text.strip().startswith("if"):
2570+
elseBranch = CGWrapper(ifFalse, pre=" else ")
2571+
else:
2572+
elseBranch = CGWrapper(CGIndenter(ifFalse), pre=" else {\n", post="\n}")
2573+
kids = [CGIfWrapper(condition, ifTrue), elseBranch]
25692574
CGList.__init__(self, kids)
25702575

25712576

@@ -2758,6 +2763,15 @@ def define(self):
27582763
if self.returnType == "void":
27592764
pre = "wrap_panic(&mut || {\n"
27602765
post = "\n})"
2766+
elif "return" not in body.define():
2767+
pre = (
2768+
"let mut result = false;\n"
2769+
"wrap_panic(&mut || result = {\n"
2770+
)
2771+
post = (
2772+
"\n});\n"
2773+
"result"
2774+
)
27612775
else:
27622776
pre = (
27632777
"let mut result = false;\n"
@@ -3225,7 +3239,7 @@ def definition_body(self):
32253239
if !get_${name}(cx, obj, this, JSJitGetterCallArgs { _base: temp.handle_mut().into() }) {
32263240
return false;
32273241
}
3228-
if !JS_DefineProperty(cx, result.handle().into(),
3242+
if !JS_DefineProperty(cx, result.handle(),
32293243
${nameAsArray} as *const u8 as *const libc::c_char,
32303244
temp.handle(), JSPROP_ENUMERATE as u32) {
32313245
return false;
@@ -6125,7 +6139,7 @@ def definition_body(self):
61256139
constructorCall = CGGeneric("""dom::bindings::htmlconstructor::call_html_constructor::<dom::types::%s>(
61266140
cx,
61276141
&args,
6128-
&*global,
6142+
&global,
61296143
PrototypeList::ID::%s,
61306144
CreateInterfaceObjects,
61316145
)""" % (self.descriptor.name, MakeNativeName(self.descriptor.name)))
@@ -6947,7 +6961,7 @@ def __init__(self, config, prefix, webIDLFile):
69476961
imports=['crate::dom::bindings::import::base::*'], config=config)
69486962

69496963
# Add the auto-generated comment.
6950-
curr = CGWrapper(curr, pre=AUTOGENERATED_WARNING_COMMENT + IGNORED_WARNINGS)
6964+
curr = CGWrapper(curr, pre=AUTOGENERATED_WARNING_COMMENT + ALLOWED_WARNINGS)
69516965

69526966
# Store the final result.
69536967
self.root = curr
@@ -7294,9 +7308,8 @@ def __init__(self, sig, name, descriptorProvider, needThisHandling):
72947308
# Check for variadic arguments
72957309
lastArg = args[self.argCount - 1]
72967310
if lastArg.variadic:
7297-
argCount = "0" if self.argCount == 1 else f"({self.argCount} - 1)"
72987311
self.argCountStr = (
7299-
"%s + %s.len()" % (argCount, lastArg.identifier.name))
7312+
"%s + %s.len()" % (self.argCount - 1, lastArg.identifier.name)).removeprefix("0 + ")
73007313
else:
73017314
self.argCountStr = "%d" % self.argCount
73027315
self.needThisHandling = needThisHandling
@@ -7397,15 +7410,15 @@ def getArgConversion(self, i, arg):
73977410
else:
73987411
jsvalIndex = "%d" % i
73997412
if arg.optional and not arg.defaultValue:
7400-
argval += ".clone().unwrap()"
7413+
argval += ".unwrap()"
74017414

74027415
conversion = wrapForType(
74037416
"argv_root.handle_mut()", result=argval,
74047417
successCode=("{\n"
74057418
"let arg = &mut argv[%s];\n"
74067419
"*arg = Heap::default();\n"
74077420
"arg.set(argv_root.get());\n"
7408-
"}") % jsvalIndex,
7421+
"}") % jsvalIndex.removeprefix("0 + "),
74097422
pre="rooted!(in(*cx) let mut argv_root = UndefinedValue());")
74107423
if arg.variadic:
74117424
conversion = string.Template(
@@ -7612,14 +7625,14 @@ def __init__(self, descriptor, likeable, methodName):
76127625
elif methodName in ["size", "clear"]: # zero arguments
76137626
CGGeneric.__init__(self, fill(
76147627
"""
7615-
let result = ${trt}::${method}(&*this);
7628+
let result = ${trt}::${method}(this);
76167629
""",
76177630
trt=trait,
76187631
method=methodName.lower()))
76197632
elif methodName == "add": # special case one argumet
76207633
CGGeneric.__init__(self, fill(
76217634
"""
7622-
${trt}::${method}(&*this, arg0);
7635+
${trt}::${method}(this, arg0);
76237636
// Returns itself per https://webidl.spec.whatwg.org/#es-set-add
76247637
let result = this;
76257638
""",
@@ -7628,14 +7641,14 @@ def __init__(self, descriptor, likeable, methodName):
76287641
elif methodName in ["has", "delete", "get"]: # one argument
76297642
CGGeneric.__init__(self, fill(
76307643
"""
7631-
let result = ${trt}::${method}(&*this, arg0);
7644+
let result = ${trt}::${method}(this, arg0);
76327645
""",
76337646
trt=trait,
76347647
method=methodName))
76357648
elif methodName == "set": # two arguments
76367649
CGGeneric.__init__(self, fill(
76377650
"""
7638-
${trt}::${method}(&*this, arg0, arg1);
7651+
${trt}::${method}(this, arg0, arg1);
76397652
// Returns itself per https://webidl.spec.whatwg.org/#es-map-set
76407653
let result = this;
76417654
""",
@@ -7663,7 +7676,7 @@ def __init__(self, descriptor, iterable, methodName):
76637676
rooted!(in(*cx) let arg0 = ObjectValue(arg0));
76647677
rooted!(in(*cx) let mut call_arg1 = UndefinedValue());
76657678
rooted!(in(*cx) let mut call_arg2 = UndefinedValue());
7666-
let mut call_args = vec![UndefinedValue(), UndefinedValue(), ObjectValue(*_obj)];
7679+
let mut call_args = [UndefinedValue(), UndefinedValue(), ObjectValue(*_obj)];
76677680
rooted!(in(*cx) let mut ignoredReturnVal = UndefinedValue());
76687681
76697682
// This has to be a while loop since get_iterable_length() may change during
@@ -7694,7 +7707,7 @@ def __init__(self, descriptor, iterable, methodName):
76947707
return
76957708
CGGeneric.__init__(self, fill(
76967709
"""
7697-
let result = ${iterClass}::new(&*this, IteratorType::${itrMethod});
7710+
let result = ${iterClass}::new(this, IteratorType::${itrMethod});
76987711
""",
76997712
iterClass=iteratorNativeType(descriptor, True),
77007713
ifaceName=descriptor.interface.identifier.name,

components/script/dom/bindings/mod.rs

+10-2
Original file line numberDiff line numberDiff line change
@@ -176,17 +176,25 @@ pub mod codegen {
176176
pub mod InterfaceObjectMap {
177177
include!(concat!(env!("OUT_DIR"), "/InterfaceObjectMap.rs"));
178178
}
179-
#[allow(dead_code, unused_imports)]
179+
#[allow(dead_code, unused_imports, clippy::enum_variant_names)]
180180
pub mod InheritTypes {
181181
include!(concat!(env!("OUT_DIR"), "/InheritTypes.rs"));
182182
}
183+
#[allow(clippy::upper_case_acronyms)]
183184
pub mod PrototypeList {
184185
include!(concat!(env!("OUT_DIR"), "/PrototypeList.rs"));
185186
}
186187
pub mod RegisterBindings {
187188
include!(concat!(env!("OUT_DIR"), "/RegisterBindings.rs"));
188189
}
189-
#[allow(non_camel_case_types, unused_imports, unused_variables)]
190+
#[allow(
191+
non_camel_case_types,
192+
unused_imports,
193+
unused_variables,
194+
clippy::large_enum_variant,
195+
clippy::upper_case_acronyms,
196+
clippy::enum_variant_names
197+
)]
190198
pub mod UnionTypes {
191199
include!(concat!(env!("OUT_DIR"), "/UnionTypes.rs"));
192200
}

components/script/dom/event.rs

+2-5
Original file line numberDiff line numberDiff line change
@@ -755,10 +755,7 @@ fn inner_invoke(
755755
}
756756

757757
impl Default for EventBinding::EventInit {
758-
fn default() -> EventBinding::EventInit {
759-
EventBinding::EventInit {
760-
bubbles: false,
761-
cancelable: false,
762-
}
758+
fn default() -> Self {
759+
Self::empty()
763760
}
764761
}

components/script/dom/extendableevent.rs

+3-5
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use dom_struct::dom_struct;
66
use js::rust::{HandleObject, HandleValue};
77
use servo_atoms::Atom;
88

9-
use crate::dom::bindings::codegen::Bindings::EventBinding::{self, EventMethods};
9+
use crate::dom::bindings::codegen::Bindings::EventBinding::EventMethods;
1010
use crate::dom::bindings::codegen::Bindings::ExtendableEventBinding;
1111
use crate::dom::bindings::error::{Error, ErrorResult, Fallible};
1212
use crate::dom::bindings::inheritance::Castable;
@@ -94,9 +94,7 @@ impl ExtendableEvent {
9494
}
9595

9696
impl Default for ExtendableEventBinding::ExtendableEventInit {
97-
fn default() -> ExtendableEventBinding::ExtendableEventInit {
98-
ExtendableEventBinding::ExtendableEventInit {
99-
parent: EventBinding::EventInit::default(),
100-
}
97+
fn default() -> Self {
98+
Self::empty()
10199
}
102100
}

0 commit comments

Comments
 (0)