From: Jason Ish Date: Mon, 28 Nov 2022 21:50:24 +0000 (-0600) Subject: rust/clippy: fix lint: needless_range_loop X-Git-Tag: suricata-7.0.0-rc1~291 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=3044565cf488a4983f06779e17b7a1eef86727a3;p=thirdparty%2Fsuricata.git rust/clippy: fix lint: needless_range_loop --- diff --git a/rust/src/http2/detect.rs b/rust/src/http2/detect.rs index c7b556a9e1..5ed5cd1c44 100644 --- a/rust/src/http2/detect.rs +++ b/rust/src/http2/detect.rs @@ -243,14 +243,14 @@ pub unsafe extern "C" fn rs_http2_detect_settingsctx_free(ctx: *mut std::os::raw fn http2_detect_settings_match( set: &[parser::HTTP2FrameSettings], ctx: &parser::DetectHTTP2settingsSigCtx, ) -> std::os::raw::c_int { - for i in 0..set.len() { - if set[i].id == ctx.id { + for e in set { + if e.id == ctx.id { match &ctx.value { None => { return 1; } Some(x) => { - if detect_match_uint(x, set[i].value) { + if detect_match_uint(x, e.value) { return 1; } } @@ -399,8 +399,8 @@ fn http2_frames_get_header_firstvalue<'a>( } else { &tx.frames_tc }; - for i in 0..frames.len() { - if let Some(blocks) = http2_header_blocks(&frames[i]) { + for frame in frames { + if let Some(blocks) = http2_header_blocks(frame) { for block in blocks.iter() { if block.name == name.as_bytes() { return Ok(&block.value); @@ -423,8 +423,8 @@ pub fn http2_frames_get_header_value_vec( } else { &tx.frames_tc }; - for i in 0..frames.len() { - if let Some(blocks) = http2_header_blocks(&frames[i]) { + for frame in frames { + if let Some(blocks) = http2_header_blocks(frame) { for block in blocks.iter() { if block.name == name.as_bytes() { if found == 0 { @@ -460,8 +460,8 @@ fn http2_frames_get_header_value<'a>( } else { &tx.frames_tc }; - for i in 0..frames.len() { - if let Some(blocks) = http2_header_blocks(&frames[i]) { + for frame in frames { + if let Some(blocks) = http2_header_blocks(frame) { for block in blocks.iter() { if block.name == name.as_bytes() { if found == 0 { @@ -536,8 +536,8 @@ fn http2_lower(value: &[u8]) -> Option> { // we got at least one upper character, need to transform let mut vec: Vec = Vec::with_capacity(value.len()); vec.extend_from_slice(value); - for j in i..vec.len() { - vec[j].make_ascii_lowercase(); + for e in &mut vec { + e.make_ascii_lowercase(); } return Some(vec); } @@ -674,8 +674,8 @@ pub unsafe extern "C" fn rs_http2_tx_get_header_names( } else { &tx.frames_tc }; - for i in 0..frames.len() { - if let Some(blocks) = http2_header_blocks(&frames[i]) { + for frame in frames { + if let Some(blocks) = http2_header_blocks(frame) { for block in blocks.iter() { // we do not escape linefeeds in headers names vec.extend_from_slice(&block.name); @@ -738,8 +738,8 @@ pub unsafe extern "C" fn rs_http2_tx_get_headers( } else { &tx.frames_tc }; - for i in 0..frames.len() { - if let Some(blocks) = http2_header_blocks(&frames[i]) { + for frame in frames { + if let Some(blocks) = http2_header_blocks(frame) { for block in blocks.iter() { if !http2_header_iscookie(direction.into(), &block.name) { // we do not escape linefeeds nor : in headers names @@ -772,8 +772,8 @@ pub unsafe extern "C" fn rs_http2_tx_get_headers_raw( } else { &tx.frames_tc }; - for i in 0..frames.len() { - if let Some(blocks) = http2_header_blocks(&frames[i]) { + for frame in frames { + if let Some(blocks) = http2_header_blocks(frame) { for block in blocks.iter() { // we do not escape linefeeds nor : in headers names vec.extend_from_slice(&block.name); diff --git a/rust/src/http2/http2.rs b/rust/src/http2/http2.rs index 44017ea8f5..1b75833184 100644 --- a/rust/src/http2/http2.rs +++ b/rust/src/http2/http2.rs @@ -195,10 +195,10 @@ impl HTTP2Transaction { self.tx_data.set_event(event as u8); } - fn handle_headers(&mut self, blocks: &Vec, dir: Direction) { - for i in 0..blocks.len() { - if blocks[i].name == b"content-encoding" { - self.decoder.http2_encoding_fromvec(&blocks[i].value, dir); + fn handle_headers(&mut self, blocks: &[parser::HTTP2FrameHeaderBlock], dir: Direction) { + for block in blocks { + if block.name == b"content-encoding" { + self.decoder.http2_encoding_fromvec(&block.value, dir); } } } @@ -617,17 +617,17 @@ impl HTTP2State { fn process_headers(&mut self, blocks: &Vec, dir: Direction) { let (mut update, mut sizeup) = (false, 0); - for i in 0..blocks.len() { - if blocks[i].error >= parser::HTTP2HeaderDecodeStatus::HTTP2HeaderDecodeError { + for block in blocks { + if block.error >= parser::HTTP2HeaderDecodeStatus::HTTP2HeaderDecodeError { self.set_event(HTTP2Event::InvalidHeader); - } else if blocks[i].error + } else if block.error == parser::HTTP2HeaderDecodeStatus::HTTP2HeaderDecodeSizeUpdate { update = true; - if blocks[i].sizeupdate > sizeup { - sizeup = blocks[i].sizeupdate; + if block.sizeupdate > sizeup { + sizeup = block.sizeupdate; } - } else if blocks[i].error + } else if block.error == parser::HTTP2HeaderDecodeStatus::HTTP2HeaderDecodeIntegerOverflow { self.set_event(HTTP2Event::HeaderIntegerOverflow); @@ -670,16 +670,16 @@ impl HTTP2State { Some(parser::HTTP2FrameType::SETTINGS) => { match parser::http2_parse_frame_settings(input) { Ok((_, set)) => { - for i in 0..set.len() { - if set[i].id == parser::HTTP2SettingsId::SETTINGSHEADERTABLESIZE { + for e in &set { + if e.id == parser::HTTP2SettingsId::SETTINGSHEADERTABLESIZE { //reverse order as this is what we accept from the other endpoint let dyn_headers = if dir == Direction::ToClient { &mut self.dynamic_headers_ts } else { &mut self.dynamic_headers_tc }; - dyn_headers.max_size = set[i].value as usize; - if set[i].value > unsafe { HTTP2_MAX_TABLESIZE } { + dyn_headers.max_size = e.value as usize; + if e.value > unsafe { HTTP2_MAX_TABLESIZE } { //mark potential overflow dyn_headers.overflow = 1; } else { diff --git a/rust/src/http2/logger.rs b/rust/src/http2/logger.rs index 3b524a294f..cf3f30217d 100644 --- a/rust/src/http2/logger.rs +++ b/rust/src/http2/logger.rs @@ -32,44 +32,44 @@ enum HeaderName { } fn log_http2_headers<'a>( - blocks: &'a Vec, js: &mut JsonBuilder, + blocks: &'a [parser::HTTP2FrameHeaderBlock], js: &mut JsonBuilder, common: &mut HashMap>, ) -> Result<(), JsonError> { - for j in 0..blocks.len() { + for block in blocks { js.start_object()?; - match blocks[j].error { + match block.error { parser::HTTP2HeaderDecodeStatus::HTTP2HeaderDecodeSuccess => { - js.set_string_from_bytes("name", &blocks[j].name)?; - js.set_string_from_bytes("value", &blocks[j].value)?; - if let Ok(name) = std::str::from_utf8(&blocks[j].name) { + js.set_string_from_bytes("name", &block.name)?; + js.set_string_from_bytes("value", &block.value)?; + if let Ok(name) = std::str::from_utf8(&block.name) { match name.to_lowercase().as_ref() { ":method" => { - common.insert(HeaderName::Method, &blocks[j].value); + common.insert(HeaderName::Method, &block.value); } ":path" => { - common.insert(HeaderName::Path, &blocks[j].value); + common.insert(HeaderName::Path, &block.value); } ":status" => { - common.insert(HeaderName::Status, &blocks[j].value); + common.insert(HeaderName::Status, &block.value); } "user-agent" => { - common.insert(HeaderName::UserAgent, &blocks[j].value); + common.insert(HeaderName::UserAgent, &block.value); } "host" => { - common.insert(HeaderName::Host, &blocks[j].value); + common.insert(HeaderName::Host, &block.value); } "content-length" => { - common.insert(HeaderName::ContentLength, &blocks[j].value); + common.insert(HeaderName::ContentLength, &block.value); } _ => {} } } } parser::HTTP2HeaderDecodeStatus::HTTP2HeaderDecodeSizeUpdate => { - js.set_uint("table_size_update", blocks[j].sizeupdate)?; + js.set_uint("table_size_update", block.sizeupdate)?; } _ => { - js.set_string("error", &blocks[j].error.to_string())?; + js.set_string("error", &block.error.to_string())?; } } js.close()?; @@ -102,18 +102,18 @@ fn log_headers<'a>( Ok(has_headers) } -fn log_http2_frames(frames: &Vec, js: &mut JsonBuilder) -> Result { +fn log_http2_frames(frames: &[HTTP2Frame], js: &mut JsonBuilder) -> Result { let mut has_settings = false; - for i in 0..frames.len() { - if let HTTP2FrameTypeData::SETTINGS(set) = &frames[i].data { + for frame in frames { + if let HTTP2FrameTypeData::SETTINGS(set) = &frame.data { if !has_settings { js.open_array("settings")?; has_settings = true; } - for j in 0..set.len() { + for e in set { js.start_object()?; - js.set_string("settings_id", &set[j].id.to_string())?; - js.set_uint("settings_value", set[j].value as u64)?; + js.set_string("settings_id", &e.id.to_string())?; + js.set_uint("settings_value", e.value as u64)?; js.close()?; } } @@ -125,8 +125,8 @@ fn log_http2_frames(frames: &Vec, js: &mut JsonBuilder) -> Result { if !has_error_code { let errcode: Option = diff --git a/rust/src/http2/parser.rs b/rust/src/http2/parser.rs index 6c16d8762f..bd7f661d72 100644 --- a/rust/src/http2/parser.rs +++ b/rust/src/http2/parser.rs @@ -512,8 +512,8 @@ fn http2_parse_var_uint(input: &[u8], value: u64, max: u64) -> IResult<&[u8], u6 return Ok((i3, 0)); } let mut varval = max; - for i in 0..varia.len() { - varval += ((varia[i] & 0x7F) as u64) << (7 * i); + for (i, e) in varia.iter().enumerate() { + varval += ((e & 0x7F) as u64) << (7 * i); } match varval.checked_add((finalv as u64) << (7 * varia.len())) { None => { @@ -892,11 +892,8 @@ mod tests { match r1 { Ok((rem, ctx)) => { assert_eq!(ctx.id, HTTP2SettingsId::SETTINGSENABLEPUSH); - match ctx.value { - Some(_) => { - panic!("Unexpected value"); - } - None => {} + if ctx.value.is_some() { + panic!("Unexpected value"); } assert_eq!(rem.len(), 1); } diff --git a/rust/src/lib.rs b/rust/src/lib.rs index a7f588ab47..ee5810c668 100644 --- a/rust/src/lib.rs +++ b/rust/src/lib.rs @@ -37,7 +37,6 @@ #![allow(clippy::manual_find)] #![allow(clippy::match_like_matches_macro)] #![allow(clippy::module_inception)] -#![allow(clippy::needless_range_loop)] #![allow(clippy::never_loop)] #![allow(clippy::new_without_default)] #![allow(clippy::nonminimal_bool)] diff --git a/rust/src/pgsql/parser.rs b/rust/src/pgsql/parser.rs index e4fffa5fa4..637bf576fb 100644 --- a/rust/src/pgsql/parser.rs +++ b/rust/src/pgsql/parser.rs @@ -557,9 +557,9 @@ pub fn pgsql_parse_startup_parameters(i: &[u8]) -> IResult<&[u8], PgsqlStartupPa if let Some(ref mut params) = optional { let mut user = PgsqlParameter{name: PgsqlParameters::User, value: Vec::new() }; let mut index: usize = 0; - for j in 0..params.len() { - if params[j].name == PgsqlParameters::User { - user.value.extend_from_slice(¶ms[j].value); + for (j, p) in params.iter().enumerate() { + if p.name == PgsqlParameters::User { + user.value.extend_from_slice(&p.value); index = j; } }