Skip to content

Commit a5e376b

Browse files
committed
Fix easy clippy warnings
1 parent fb7e47b commit a5e376b

File tree

7 files changed

+138
-121
lines changed

7 files changed

+138
-121
lines changed

src/aml/mod.rs

Lines changed: 36 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ where
112112
table.length as usize - mem::size_of::<SdtHeader>(),
113113
)
114114
};
115-
interpreter.load_table(stream).map_err(|err| AcpiError::Aml(err))?;
115+
interpreter.load_table(stream).map_err(AcpiError::Aml)?;
116116
Ok(())
117117
}
118118

@@ -308,7 +308,7 @@ where
308308
Opcode::Increment | Opcode::Decrement => {
309309
let [Argument::Object(operand)] = &op.arguments[..] else { panic!() };
310310
let token = self.object_token.lock();
311-
let Object::Integer(operand) = (unsafe { operand.gain_mut(&*token) }) else {
311+
let Object::Integer(operand) = (unsafe { operand.gain_mut(&token) }) else {
312312
Err(AmlError::ObjectNotOfExpectedType {
313313
expected: ObjectType::Integer,
314314
got: operand.typ(),
@@ -564,7 +564,7 @@ where
564564
}
565565
Opcode::Store => {
566566
let [Argument::Object(object), target] = &op.arguments[..] else { panic!() };
567-
self.do_store(&target, object.clone())?;
567+
self.do_store(target, object.clone())?;
568568
context.retire_op(op);
569569
}
570570
Opcode::RefOf => {
@@ -788,7 +788,7 @@ where
788788
}
789789
}
790790
BlockKind::Scope { old_scope } => {
791-
assert!(context.block_stack.len() > 0);
791+
assert!(!context.block_stack.is_empty());
792792
context.current_block = context.block_stack.pop().unwrap();
793793
context.current_scope = old_scope;
794794
// Go round the loop again to get the next opcode for the new block
@@ -804,7 +804,7 @@ where
804804
* *distinct* uninitialized objects, and go round again to complete the
805805
* in-flight op.
806806
*/
807-
assert!(context.block_stack.len() > 0);
807+
assert!(!context.block_stack.is_empty());
808808

809809
if let Some(package_op) = context.in_flight.last_mut()
810810
&& (package_op.op == Opcode::Package || package_op.op == Opcode::VarPackage)
@@ -1626,7 +1626,7 @@ where
16261626
* Apparently, the NT interpreter always uses the first 8 bytes of the buffer.
16271627
*/
16281628
let mut to_interpret = [0u8; 8];
1629-
(to_interpret[0..usize::min(bytes.len(), 8)]).copy_from_slice(&bytes);
1629+
(to_interpret[0..usize::min(bytes.len(), 8)]).copy_from_slice(bytes);
16301630
Object::Integer(u64::from_le_bytes(to_interpret))
16311631
}
16321632
Object::String(ref value) => {
@@ -1642,8 +1642,9 @@ where
16421642
})?;
16431643
Object::Integer(parsed)
16441644
} else {
1645-
let parsed = u64::from_str_radix(value, 10).map_err(|_| {
1646-
AmlError::InvalidOperationOnObject { op: Operation::ToInteger, typ: ObjectType::String }
1645+
let parsed = str::parse::<u64>(value).map_err(|_| AmlError::InvalidOperationOnObject {
1646+
op: Operation::ToInteger,
1647+
typ: ObjectType::String,
16471648
})?;
16481649
Object::Integer(parsed)
16491650
}
@@ -1694,7 +1695,7 @@ where
16941695
Object::String(ref value) => Object::String(value.clone()),
16951696
Object::Integer(value) => match op.op {
16961697
Opcode::ToDecimalString => Object::String(value.to_string()),
1697-
Opcode::ToHexString => Object::String(alloc::format!("{:#x}", value)),
1698+
Opcode::ToHexString => Object::String(alloc::format!("{value:#x}")),
16981699
_ => panic!(),
16991700
},
17001701
Object::Buffer(ref bytes) => {
@@ -1704,8 +1705,8 @@ where
17041705
let mut string = String::new();
17051706
for byte in bytes {
17061707
let as_str = match op.op {
1707-
Opcode::ToDecimalString => alloc::format!("{},", byte),
1708-
Opcode::ToHexString => alloc::format!("{:#04X},", byte),
1708+
Opcode::ToDecimalString => alloc::format!("{byte},"),
1709+
Opcode::ToHexString => alloc::format!("{byte:#04X},"),
17091710
_ => panic!(),
17101711
};
17111712
string.push_str(&as_str);
@@ -1773,15 +1774,15 @@ where
17731774
fn resolve_as_string(obj: &Object) -> String {
17741775
match obj {
17751776
Object::Uninitialized => "[Uninitialized Object]".to_string(),
1776-
Object::Buffer(bytes) => String::from_utf8_lossy(&bytes).into_owned(),
1777+
Object::Buffer(bytes) => String::from_utf8_lossy(bytes).into_owned(),
17771778
Object::BufferField { .. } => "[Buffer Field]".to_string(),
17781779
Object::Device => "[Device]".to_string(),
17791780
Object::Event => "[Event]".to_string(),
17801781
Object::FieldUnit(_) => "[Field]".to_string(),
17811782
Object::Integer(value) => value.to_string(),
17821783
Object::Method { .. } | Object::NativeMethod { .. } => "[Control Method]".to_string(),
17831784
Object::Mutex { .. } => "[Mutex]".to_string(),
1784-
Object::Reference { inner, .. } => resolve_as_string(&*(inner.clone().unwrap_reference())),
1785+
Object::Reference { inner, .. } => resolve_as_string(&(inner.clone().unwrap_reference())),
17851786
Object::OpRegion(_) => "[Operation Region]".to_string(),
17861787
Object::Package(_) => "[Package]".to_string(),
17871788
Object::PowerResource { .. } => "[Power Resource]".to_string(),
@@ -1813,7 +1814,7 @@ where
18131814
buffer.extend(source2.to_buffer(if self.dsdt_revision >= 2 { 8 } else { 4 })?);
18141815
Object::Buffer(buffer).wrap()
18151816
}
1816-
ObjectType::String | _ => {
1817+
_ => {
18171818
let source1 = resolve_as_string(&source1);
18181819
let source2 = resolve_as_string(&source2);
18191820
Object::String(source1 + &source2).wrap()
@@ -1944,14 +1945,14 @@ where
19441945
let to_return = object.clone();
19451946

19461947
match target {
1947-
Argument::Object(target) => match unsafe { target.gain_mut(&*token) } {
1948-
Object::Integer(target) => match unsafe { object.gain_mut(&*token) } {
1948+
Argument::Object(target) => match unsafe { target.gain_mut(&token) } {
1949+
Object::Integer(target) => match unsafe { object.gain_mut(&token) } {
19491950
Object::Integer(value) => {
19501951
*target = *value;
19511952
}
19521953
Object::BufferField { .. } => {
19531954
let mut buffer = [0u8; 8];
1954-
unsafe { object.gain_mut(&*token) }.read_buffer_field(&mut buffer)?;
1955+
unsafe { object.gain_mut(&token) }.read_buffer_field(&mut buffer)?;
19551956
let value = u64::from_le_bytes(buffer);
19561957
*target = value;
19571958
}
@@ -1964,12 +1965,12 @@ where
19641965
*target = as_integer;
19651966
}
19661967
},
1967-
Object::BufferField { .. } => match unsafe { object.gain_mut(&*token) } {
1968+
Object::BufferField { .. } => match unsafe { object.gain_mut(&token) } {
19681969
Object::Integer(value) => {
1969-
unsafe { target.gain_mut(&*token) }.write_buffer_field(&value.to_le_bytes(), &*token)?;
1970+
unsafe { target.gain_mut(&token) }.write_buffer_field(&value.to_le_bytes(), &token)?;
19701971
}
19711972
Object::Buffer(value) => {
1972-
unsafe { target.gain_mut(&*token) }.write_buffer_field(&value.as_slice(), &*token)?;
1973+
unsafe { target.gain_mut(&token) }.write_buffer_field(value.as_slice(), &token)?;
19731974
}
19741975
_ => panic!(),
19751976
},
@@ -1982,20 +1983,20 @@ where
19821983
// TODO: this should store into the reference, potentially doing an
19831984
// implicit cast
19841985
unsafe {
1985-
*inner_inner.gain_mut(&*token) = object.gain_mut(&*token).clone();
1986+
*inner_inner.gain_mut(&token) = object.gain_mut(&token).clone();
19861987
}
19871988
} else {
19881989
// Overwrite the value
19891990
unsafe {
1990-
*inner.gain_mut(&*token) = object.gain_mut(&*token).clone();
1991+
*inner.gain_mut(&token) = object.gain_mut(&token).clone();
19911992
}
19921993
}
19931994
}
19941995
ReferenceKind::Unresolved => todo!(),
19951996
}
19961997
}
19971998
Object::Debug => {
1998-
self.handler.handle_debug(&*object);
1999+
self.handler.handle_debug(&object);
19992000
}
20002001
_ => panic!("Stores to objects like {:?} are not yet supported", target),
20012002
},
@@ -2090,7 +2091,7 @@ where
20902091

20912092
let value_bytes = match &*value {
20922093
Object::Integer(value) => &value.to_le_bytes() as &[u8],
2093-
Object::Buffer(bytes) => &bytes,
2094+
Object::Buffer(bytes) => bytes,
20942095
_ => Err(AmlError::ObjectNotOfExpectedType { expected: ObjectType::Integer, got: value.typ() })?,
20952096
};
20962097
let access_width_bits = field.flags.access_type_bytes()? * 8;
@@ -2173,7 +2174,7 @@ where
21732174
1 => self.handler.read_u8(address) as u64,
21742175
2 => self.handler.read_u16(address) as u64,
21752176
4 => self.handler.read_u32(address) as u64,
2176-
8 => self.handler.read_u64(address) as u64,
2177+
8 => self.handler.read_u64(address),
21772178
_ => panic!(),
21782179
}
21792180
}),
@@ -2230,7 +2231,7 @@ where
22302231
);
22312232

22322233
match region.space {
2233-
RegionSpace::SystemMemory => Ok({
2234+
RegionSpace::SystemMemory => {
22342235
let address = region.base as usize + offset;
22352236
match length {
22362237
1 => self.handler.write_u8(address, value as u8),
@@ -2239,16 +2240,18 @@ where
22392240
8 => self.handler.write_u64(address, value),
22402241
_ => panic!(),
22412242
}
2242-
}),
2243-
RegionSpace::SystemIO => Ok({
2243+
Ok(())
2244+
}
2245+
RegionSpace::SystemIO => {
22442246
let address = region.base as u16 + offset as u16;
22452247
match length {
22462248
1 => self.handler.write_io_u8(address, value as u8),
22472249
2 => self.handler.write_io_u16(address, value as u16),
22482250
4 => self.handler.write_io_u32(address, value as u32),
22492251
_ => panic!(),
22502252
}
2251-
}),
2253+
Ok(())
2254+
}
22522255
RegionSpace::PciConfig => {
22532256
let address = self.pci_address_for_device(&region.parent_device_path)?;
22542257
match length {
@@ -2434,10 +2437,10 @@ impl MethodContext {
24342437
}
24352438

24362439
fn contribute_arg(&mut self, arg: Argument) {
2437-
if let Some(in_flight) = self.in_flight.last_mut() {
2438-
if in_flight.arguments.len() < in_flight.expected_arguments {
2439-
in_flight.arguments.push(arg);
2440-
}
2440+
if let Some(in_flight) = self.in_flight.last_mut()
2441+
&& in_flight.arguments.len() < in_flight.expected_arguments
2442+
{
2443+
in_flight.arguments.push(arg);
24412444
}
24422445
}
24432446

src/aml/namespace.rs

Lines changed: 42 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@ use alloc::{
1010
vec::Vec,
1111
};
1212
use bit_field::BitField;
13-
use core::{fmt, str, str::FromStr};
13+
use core::{
14+
fmt,
15+
str::{self, FromStr},
16+
};
1417
use log::{trace, warn};
1518

1619
#[derive(Clone)]
@@ -257,10 +260,10 @@ impl Namespace {
257260

258261
loop {
259262
let name = level_name.resolve(&scope)?;
260-
if let Ok((level, last_seg)) = self.get_level_for_path(&name) {
261-
if level.children.contains_key(&last_seg) {
262-
return Ok(name);
263-
}
263+
if let Ok((level, last_seg)) = self.get_level_for_path(&name)
264+
&& level.children.contains_key(&last_seg)
265+
{
266+
return Ok(name);
264267
}
265268

266269
// If we don't find it, move the scope up a level and search for it there recursively
@@ -296,7 +299,7 @@ impl Namespace {
296299
panic!();
297300
};
298301
current_level =
299-
current_level.children.get(&segment).ok_or(AmlError::LevelDoesNotExist(traversed_path.clone()))?;
302+
current_level.children.get(segment).ok_or(AmlError::LevelDoesNotExist(traversed_path.clone()))?;
300303
}
301304

302305
Ok((current_level, *last_seg))
@@ -326,7 +329,7 @@ impl Namespace {
326329
};
327330
current_level = current_level
328331
.children
329-
.get_mut(&segment)
332+
.get_mut(segment)
330333
.ok_or(AmlError::LevelDoesNotExist(traversed_path.clone()))?;
331334
}
332335

@@ -369,12 +372,7 @@ impl fmt::Display for Namespace {
369372
const BRANCH: &str = "├── ";
370373
const END: &str = "└── ";
371374

372-
fn print_level(
373-
namespace: &Namespace,
374-
f: &mut fmt::Formatter<'_>,
375-
level: &NamespaceLevel,
376-
indent_stack: String,
377-
) -> fmt::Result {
375+
fn print_level(f: &mut fmt::Formatter<'_>, level: &NamespaceLevel, indent_stack: String) -> fmt::Result {
378376
for (i, (name, (flags, object))) in level.values.iter().enumerate() {
379377
let end = (i == level.values.len() - 1)
380378
&& level.children.iter().filter(|(_, l)| l.kind == NamespaceLevelKind::Scope).count() == 0;
@@ -389,9 +387,8 @@ impl fmt::Display for Namespace {
389387
)?;
390388

391389
// If the object has a corresponding scope, print it here
392-
if let Some(child_level) = level.children.get(&name) {
390+
if let Some(child_level) = level.children.get(name) {
393391
print_level(
394-
namespace,
395392
f,
396393
child_level,
397394
if end { indent_stack.clone() + " " } else { indent_stack.clone() + STEM },
@@ -404,14 +401,14 @@ impl fmt::Display for Namespace {
404401
for (i, (name, sub_level)) in remaining_scopes.iter().enumerate() {
405402
let end = i == remaining_scopes.len() - 1;
406403
writeln!(f, "{}{}{}:", &indent_stack, if end { END } else { BRANCH }, name.as_str())?;
407-
print_level(namespace, f, sub_level, indent_stack.clone() + STEM)?;
404+
print_level(f, sub_level, indent_stack.clone() + STEM)?;
408405
}
409406

410407
Ok(())
411408
}
412409

413410
writeln!(f, "\n \\:")?;
414-
print_level(self, f, &self.root, String::from(" "))
411+
print_level(f, &self.root, String::from(" "))
415412
}
416413
}
417414

@@ -615,15 +612,40 @@ impl fmt::Display for AmlName {
615612
pub struct NameSeg(pub(crate) [u8; 4]);
616613

617614
impl NameSeg {
618-
pub fn from_str(string: &str) -> Result<NameSeg, AmlError> {
615+
pub fn from_bytes(bytes: [u8; 4]) -> Result<NameSeg, AmlError> {
616+
if !is_lead_name_char(bytes[0]) {
617+
return Err(AmlError::InvalidNameSeg(bytes));
618+
}
619+
if !is_name_char(bytes[1]) {
620+
return Err(AmlError::InvalidNameSeg(bytes));
621+
}
622+
if !is_name_char(bytes[2]) {
623+
return Err(AmlError::InvalidNameSeg(bytes));
624+
}
625+
if !is_name_char(bytes[3]) {
626+
return Err(AmlError::InvalidNameSeg(bytes));
627+
}
628+
Ok(NameSeg(bytes))
629+
}
630+
631+
pub fn as_str(&self) -> &str {
632+
// We should only construct valid ASCII name segments
633+
unsafe { str::from_utf8_unchecked(&self.0) }
634+
}
635+
}
636+
637+
impl FromStr for NameSeg {
638+
type Err = AmlError;
639+
640+
fn from_str(s: &str) -> Result<Self, Self::Err> {
619641
// Each NameSeg can only have four chars, and must have at least one
620-
if string.is_empty() || string.len() > 4 {
642+
if s.is_empty() || s.len() > 4 {
621643
return Err(AmlError::InvalidNameSeg([0xff, 0xff, 0xff, 0xff]));
622644
}
623645

624646
// We pre-fill the array with '_', so it will already be correct if the length is < 4
625647
let mut seg = [b'_'; 4];
626-
let bytes = string.as_bytes();
648+
let bytes = s.as_bytes();
627649

628650
// Manually do the first one, because we have to check it's a LeadNameChar
629651
if !is_lead_name_char(bytes[0]) {
@@ -641,27 +663,6 @@ impl NameSeg {
641663

642664
Ok(NameSeg(seg))
643665
}
644-
645-
pub fn from_bytes(bytes: [u8; 4]) -> Result<NameSeg, AmlError> {
646-
if !is_lead_name_char(bytes[0]) {
647-
return Err(AmlError::InvalidNameSeg(bytes));
648-
}
649-
if !is_name_char(bytes[1]) {
650-
return Err(AmlError::InvalidNameSeg(bytes));
651-
}
652-
if !is_name_char(bytes[2]) {
653-
return Err(AmlError::InvalidNameSeg(bytes));
654-
}
655-
if !is_name_char(bytes[3]) {
656-
return Err(AmlError::InvalidNameSeg(bytes));
657-
}
658-
Ok(NameSeg(bytes))
659-
}
660-
661-
pub fn as_str(&self) -> &str {
662-
// We should only construct valid ASCII name segments
663-
unsafe { str::from_utf8_unchecked(&self.0) }
664-
}
665666
}
666667

667668
pub fn is_lead_name_char(c: u8) -> bool {

0 commit comments

Comments
 (0)