From da12b77f18a4630b74fff7e958bf2980e6076b0d Mon Sep 17 00:00:00 2001 From: Jason Ish Date: Mon, 28 Nov 2022 17:20:40 -0600 Subject: [PATCH] rust/clippy: fix lint: new_without_default --- rust/src/applayertemplate/template.rs | 17 +++++++-------- rust/src/bittorrent_dht/bittorrent_dht.rs | 16 ++++----------- rust/src/dcerpc/detect.rs | 10 +++++++-- rust/src/dns/dns.rs | 18 ++++------------ rust/src/http2/http2.rs | 22 ++++++++++++++++++-- rust/src/ike/ike.rs | 25 ++++++++++------------- rust/src/krb/detect.rs | 10 +++++++-- rust/src/krb/krb5.rs | 8 +++++++- rust/src/lib.rs | 1 - rust/src/modbus/modbus.rs | 8 ++------ rust/src/mqtt/mqtt.rs | 6 ++++++ rust/src/nfs/nfs.rs | 12 +++++++++++ rust/src/ntp/ntp.rs | 12 ++++------- rust/src/pgsql/pgsql.rs | 16 +++++++++++++-- rust/src/rfb/rfb.rs | 16 +++++++++++++-- rust/src/sip/sip.rs | 7 ++----- rust/src/smb/smb.rs | 11 ++++++---- rust/src/snmp/snmp.rs | 8 ++------ rust/src/ssh/ssh.rs | 21 ++++++++++--------- rust/src/telnet/telnet.rs | 14 ++++++++----- 20 files changed, 154 insertions(+), 104 deletions(-) diff --git a/rust/src/applayertemplate/template.rs b/rust/src/applayertemplate/template.rs index a69475a5fb..14ea6ff7ab 100644 --- a/rust/src/applayertemplate/template.rs +++ b/rust/src/applayertemplate/template.rs @@ -37,9 +37,15 @@ pub struct TemplateTransaction { tx_data: AppLayerTxData, } +impl Default for TemplateTransaction { + fn default() -> Self { + Self::new() + } +} + impl TemplateTransaction { pub fn new() -> TemplateTransaction { - TemplateTransaction { + Self { tx_id: 0, request: None, response: None, @@ -54,6 +60,7 @@ impl Transaction for TemplateTransaction { } } +#[derive(Default)] pub struct TemplateState { state_data: AppLayerStateData, tx_id: u64, @@ -74,13 +81,7 @@ impl State for TemplateState { impl TemplateState { pub fn new() -> Self { - Self { - state_data: AppLayerStateData::new(), - tx_id: 0, - transactions: VecDeque::new(), - request_gap: false, - response_gap: false, - } + Default::default() } // Free a transaction by ID. diff --git a/rust/src/bittorrent_dht/bittorrent_dht.rs b/rust/src/bittorrent_dht/bittorrent_dht.rs index c06bfb0dce..10b2498388 100644 --- a/rust/src/bittorrent_dht/bittorrent_dht.rs +++ b/rust/src/bittorrent_dht/bittorrent_dht.rs @@ -32,6 +32,7 @@ pub enum BitTorrentDHTEvent { MalformedPacket, } +#[derive(Default)] pub struct BitTorrentDHTTransaction { tx_id: u64, pub request_type: Option, @@ -45,17 +46,8 @@ pub struct BitTorrentDHTTransaction { } impl BitTorrentDHTTransaction { - pub fn new() -> BitTorrentDHTTransaction { - BitTorrentDHTTransaction { - tx_id: 0, - request_type: None, - request: None, - response: None, - error: None, - transaction_id: Vec::new(), - client_version: None, - tx_data: AppLayerTxData::new(), - } + pub fn new() -> Self { + Self::default() } /// Set an event on the transaction @@ -86,7 +78,7 @@ impl BitTorrentDHTState { } fn new_tx(&mut self) -> BitTorrentDHTTransaction { - let mut tx = BitTorrentDHTTransaction::new(); + let mut tx = BitTorrentDHTTransaction::default(); self.tx_id += 1; tx.tx_id = self.tx_id; return tx; diff --git a/rust/src/dcerpc/detect.rs b/rust/src/dcerpc/detect.rs index ab9c637ab8..e0857f56be 100644 --- a/rust/src/dcerpc/detect.rs +++ b/rust/src/dcerpc/detect.rs @@ -39,12 +39,18 @@ pub struct DCEOpnumRange { pub range2: u32, } +impl Default for DCEOpnumRange { + fn default() -> Self { + Self::new() + } +} + impl DCEOpnumRange { pub fn new() -> Self { - return Self { + Self { range1: DETECT_DCE_OPNUM_RANGE_UNINITIALIZED, range2: DETECT_DCE_OPNUM_RANGE_UNINITIALIZED, - }; + } } } diff --git a/rust/src/dns/dns.rs b/rust/src/dns/dns.rs index 4a52d8ee40..c9eb443060 100644 --- a/rust/src/dns/dns.rs +++ b/rust/src/dns/dns.rs @@ -235,7 +235,7 @@ pub struct DNSResponse { pub authorities: Vec, } -#[derive(Debug)] +#[derive(Debug, Default)] pub struct DNSTransaction { pub id: u64, pub request: Option, @@ -250,14 +250,8 @@ impl Transaction for DNSTransaction { } impl DNSTransaction { - pub fn new() -> Self { - return Self { - id: 0, - request: None, - response: None, - tx_data: AppLayerTxData::new(), - } + Default::default() } /// Get the DNS transactions ID (not the internal tracking ID). @@ -342,11 +336,7 @@ impl State for DNSState { impl DNSState { pub fn new() -> Self { - Default::default() - } - - pub fn new_tcp() -> Self { - Default::default() + Default::default() } pub fn new_tx(&mut self) -> DNSTransaction { @@ -684,7 +674,7 @@ pub extern "C" fn rs_dns_state_new(_orig_state: *mut std::os::raw::c_void, _orig /// Returns *mut DNSState #[no_mangle] pub extern "C" fn rs_dns_state_tcp_new() -> *mut std::os::raw::c_void { - let state = DNSState::new_tcp(); + let state = DNSState::new(); let boxed = Box::new(state); return Box::into_raw(boxed) as *mut _; } diff --git a/rust/src/http2/http2.rs b/rust/src/http2/http2.rs index 1b75833184..9f4fc7f30a 100644 --- a/rust/src/http2/http2.rs +++ b/rust/src/http2/http2.rs @@ -152,9 +152,15 @@ impl Transaction for HTTP2Transaction { } } +impl Default for HTTP2Transaction { + fn default() -> Self { + Self::new() + } +} + impl HTTP2Transaction { - pub fn new() -> HTTP2Transaction { - HTTP2Transaction { + pub fn new() -> Self { + Self { tx_id: 0, stream_id: 0, child_stream_id: 0, @@ -386,6 +392,12 @@ pub struct HTTP2DynTable { pub overflow: u8, } +impl Default for HTTP2DynTable { + fn default() -> Self { + Self::new() + } +} + impl HTTP2DynTable { pub fn new() -> Self { Self { @@ -418,6 +430,12 @@ impl State for HTTP2State { } } +impl Default for HTTP2State { + fn default() -> Self { + Self::new() + } +} + impl HTTP2State { pub fn new() -> Self { Self { diff --git a/rust/src/ike/ike.rs b/rust/src/ike/ike.rs index 8064df4709..6ed1896d78 100644 --- a/rust/src/ike/ike.rs +++ b/rust/src/ike/ike.rs @@ -60,9 +60,15 @@ pub struct IkeHeaderWrapper { pub ikev2_header: IkeV2Header, } +impl Default for IkeHeaderWrapper { + fn default() -> Self { + Self::new() + } +} + impl IkeHeaderWrapper { - pub fn new() -> IkeHeaderWrapper { - IkeHeaderWrapper { + pub fn new() -> Self { + Self { spi_initiator: String::new(), spi_responder: String::new(), maj_ver: 0, @@ -93,6 +99,7 @@ pub struct IkePayloadWrapper { pub ikev2_payload_types: Vec, } +#[derive(Default)] pub struct IKETransaction { tx_id: u64, @@ -116,18 +123,8 @@ impl Transaction for IKETransaction { } impl IKETransaction { - pub fn new() -> IKETransaction { - IKETransaction { - tx_id: 0, - ike_version: 0, - direction: Direction::ToServer, - hdr: IkeHeaderWrapper::new(), - payload_types: Default::default(), - notify_types: vec![], - logged: LoggerFlags::new(), - tx_data: applayer::AppLayerTxData::new(), - errors: 0, - } + pub fn new() -> Self { + Default::default() } /// Set an event. diff --git a/rust/src/krb/detect.rs b/rust/src/krb/detect.rs index 6274c1b37b..c66162f4b5 100644 --- a/rust/src/krb/detect.rs +++ b/rust/src/krb/detect.rs @@ -87,9 +87,15 @@ pub struct DetectKrb5TicketEncryptionList { other: Vec, } +impl Default for DetectKrb5TicketEncryptionList { + fn default() -> Self { + Self::new() + } +} + impl DetectKrb5TicketEncryptionList { - pub fn new() -> DetectKrb5TicketEncryptionList { - DetectKrb5TicketEncryptionList { + pub fn new() -> Self { + Self { positive: [false; KRB_TICKET_FASTARRAY_SIZE], negative: [false; KRB_TICKET_FASTARRAY_SIZE], other: Vec::new(), diff --git a/rust/src/krb/krb5.rs b/rust/src/krb/krb5.rs index 247dd2194b..9ae498fbbd 100644 --- a/rust/src/krb/krb5.rs +++ b/rust/src/krb/krb5.rs @@ -102,9 +102,15 @@ pub fn to_hex_string(bytes: &[u8]) -> String { s } +impl Default for KRB5State { + fn default() -> Self { + Self::new() + } +} + impl KRB5State { pub fn new() -> KRB5State { - KRB5State{ + Self { state_data: AppLayerStateData::new(), req_id: 0, record_ts: 0, diff --git a/rust/src/lib.rs b/rust/src/lib.rs index d3b989c429..dfac0f15ed 100644 --- a/rust/src/lib.rs +++ b/rust/src/lib.rs @@ -34,7 +34,6 @@ #![allow(clippy::manual_find)] #![allow(clippy::match_like_matches_macro)] #![allow(clippy::module_inception)] -#![allow(clippy::new_without_default)] #![allow(clippy::result_unit_err)] #![allow(clippy::type_complexity)] #![allow(clippy::upper_case_acronyms)] diff --git a/rust/src/modbus/modbus.rs b/rust/src/modbus/modbus.rs index 2ffc24f858..3c6a699fb6 100644 --- a/rust/src/modbus/modbus.rs +++ b/rust/src/modbus/modbus.rs @@ -89,6 +89,7 @@ impl ModbusTransaction { } } +#[derive(Default)] pub struct ModbusState { state_data: AppLayerStateData, pub transactions: Vec, @@ -108,12 +109,7 @@ impl State for ModbusState { impl ModbusState { pub fn new() -> Self { - Self { - state_data: AppLayerStateData::new(), - transactions: Vec::new(), - tx_id: 0, - givenup: false, - } + Default::default() } pub fn get_tx(&mut self, tx_id: u64) -> Option<&mut ModbusTransaction> { diff --git a/rust/src/mqtt/mqtt.rs b/rust/src/mqtt/mqtt.rs index b55e8d2952..2e7b47fb8f 100644 --- a/rust/src/mqtt/mqtt.rs +++ b/rust/src/mqtt/mqtt.rs @@ -118,6 +118,12 @@ impl State for MQTTState { } } +impl Default for MQTTState { + fn default() -> Self { + Self::new() + } +} + impl MQTTState { pub fn new() -> Self { Self { diff --git a/rust/src/nfs/nfs.rs b/rust/src/nfs/nfs.rs index 26960cdccf..21dd0dd5d2 100644 --- a/rust/src/nfs/nfs.rs +++ b/rust/src/nfs/nfs.rs @@ -211,6 +211,12 @@ pub struct NFSTransaction { pub tx_data: AppLayerTxData, } +impl Default for NFSTransaction { + fn default() -> Self { + Self::new() + } +} + impl NFSTransaction { pub fn new() -> Self { return Self { @@ -342,6 +348,12 @@ pub struct NFSState { ts: u64, } +impl Default for NFSState { + fn default() -> Self { + Self::new() + } +} + impl State for NFSState { fn get_transaction_count(&self) -> usize { self.transactions.len() diff --git a/rust/src/ntp/ntp.rs b/rust/src/ntp/ntp.rs index f6514f4cbe..6e6f775903 100644 --- a/rust/src/ntp/ntp.rs +++ b/rust/src/ntp/ntp.rs @@ -35,6 +35,7 @@ pub enum NTPEvent { NotResponse, } +#[derive(Default)] pub struct NTPState { state_data: AppLayerStateData, @@ -48,7 +49,7 @@ pub struct NTPState { tx_id: u64, } -#[derive(Debug)] +#[derive(Debug, Default)] pub struct NTPTransaction { /// The NTP reference ID pub xid: u32, @@ -66,13 +67,8 @@ impl Transaction for NTPTransaction { } impl NTPState { - pub fn new() -> NTPState { - NTPState { - state_data: AppLayerStateData::new(), - transactions: Vec::new(), - events: 0, - tx_id: 0, - } + pub fn new() -> Self { + Default::default() } } diff --git a/rust/src/pgsql/pgsql.rs b/rust/src/pgsql/pgsql.rs index 0a0eaaaf4b..4411827373 100644 --- a/rust/src/pgsql/pgsql.rs +++ b/rust/src/pgsql/pgsql.rs @@ -62,9 +62,15 @@ impl Transaction for PgsqlTransaction { } } +impl Default for PgsqlTransaction { + fn default() -> Self { + Self::new() + } +} + impl PgsqlTransaction { - pub fn new() -> PgsqlTransaction { - PgsqlTransaction { + pub fn new() -> Self { + Self { tx_id: 0, tx_state: PgsqlTransactionState::Init, request: None, @@ -140,6 +146,12 @@ impl State for PgsqlState { } } +impl Default for PgsqlState { + fn default() -> Self { + Self::new() + } +} + impl PgsqlState { pub fn new() -> Self { Self { diff --git a/rust/src/rfb/rfb.rs b/rust/src/rfb/rfb.rs index e579edadb1..ba5bdca8b1 100644 --- a/rust/src/rfb/rfb.rs +++ b/rust/src/rfb/rfb.rs @@ -53,9 +53,15 @@ impl Transaction for RFBTransaction { } } +impl Default for RFBTransaction { + fn default() -> Self { + Self::new() + } +} + impl RFBTransaction { - pub fn new() -> RFBTransaction { - RFBTransaction { + pub fn new() -> Self { + Self { tx_id: 0, complete: false, chosen_security_type: None, @@ -95,6 +101,12 @@ impl State for RFBState { } +impl Default for RFBState { + fn default() -> Self { + Self::new() + } +} + impl RFBState { pub fn new() -> Self { Self { diff --git a/rust/src/sip/sip.rs b/rust/src/sip/sip.rs index ff4d01200c..a52f556068 100755 --- a/rust/src/sip/sip.rs +++ b/rust/src/sip/sip.rs @@ -45,6 +45,7 @@ pub enum SIPEvent { InvalidData, } +#[derive(Default)] pub struct SIPState { state_data: AppLayerStateData, transactions: Vec, @@ -78,11 +79,7 @@ impl Transaction for SIPTransaction { impl SIPState { pub fn new() -> SIPState { - SIPState { - state_data: AppLayerStateData::new(), - transactions: Vec::new(), - tx_id: 0, - } + Default::default() } pub fn free(&mut self) { diff --git a/rust/src/smb/smb.rs b/rust/src/smb/smb.rs index 32ce5f6218..54407bbe9e 100644 --- a/rust/src/smb/smb.rs +++ b/rust/src/smb/smb.rs @@ -488,12 +488,18 @@ impl Transaction for SMBTransaction { } } +impl Default for SMBTransaction { + fn default() -> Self { + Self::new() + } +} + impl SMBTransaction { pub fn new() -> Self { return Self { id: 0, vercmd: SMBVerCmdStat::new(), - hdr: SMBCommonHdr::init(), + hdr: SMBCommonHdr::default(), request_done: false, response_done: false, type_data: None, @@ -560,9 +566,6 @@ pub struct SMBCommonHdr { } impl SMBCommonHdr { - pub fn init() -> Self { - Default::default() - } pub fn new(rec_type: u32, ssn_id: u64, tree_id: u32, msg_id: u64) -> Self { Self { rec_type, diff --git a/rust/src/snmp/snmp.rs b/rust/src/snmp/snmp.rs index 43d6ba5455..f1e58ded4a 100644 --- a/rust/src/snmp/snmp.rs +++ b/rust/src/snmp/snmp.rs @@ -36,6 +36,7 @@ pub enum SNMPEvent { VersionMismatch, } +#[derive(Default)] pub struct SNMPState<'a> { state_data: AppLayerStateData, @@ -89,12 +90,7 @@ impl<'a> Transaction for SNMPTransaction<'a> { impl<'a> SNMPState<'a> { pub fn new() -> SNMPState<'a> { - SNMPState{ - state_data: AppLayerStateData::new(), - version: 0, - transactions: Vec::new(), - tx_id: 0, - } + Default::default() } } diff --git a/rust/src/ssh/ssh.rs b/rust/src/ssh/ssh.rs index 21c4d1ad3e..aa05c59b72 100644 --- a/rust/src/ssh/ssh.rs +++ b/rust/src/ssh/ssh.rs @@ -62,9 +62,15 @@ pub struct SshHeader { pub hassh_string: Vec, } +impl Default for SshHeader { + fn default() -> Self { + Self::new() + } +} + impl SshHeader { pub fn new() -> SshHeader { - SshHeader { + Self { record_left: 0, record_left_msg: parser::MessageCode::Undefined(0), @@ -78,6 +84,7 @@ impl SshHeader { } } +#[derive(Default)] pub struct SSHTransaction { pub srv_hdr: SshHeader, pub cli_hdr: SshHeader, @@ -87,14 +94,11 @@ pub struct SSHTransaction { impl SSHTransaction { pub fn new() -> SSHTransaction { - SSHTransaction { - srv_hdr: SshHeader::new(), - cli_hdr: SshHeader::new(), - tx_data: AppLayerTxData::new(), - } + Default::default() } } +#[derive(Default)] pub struct SSHState { state_data: AppLayerStateData, transaction: SSHTransaction, @@ -102,10 +106,7 @@ pub struct SSHState { impl SSHState { pub fn new() -> Self { - Self { - state_data: AppLayerStateData::new(), - transaction: SSHTransaction::new(), - } + Default::default() } fn set_event(&mut self, event: SSHEvent) { diff --git a/rust/src/telnet/telnet.rs b/rust/src/telnet/telnet.rs index 86e3458746..75c752dd20 100644 --- a/rust/src/telnet/telnet.rs +++ b/rust/src/telnet/telnet.rs @@ -35,17 +35,15 @@ pub enum TelnetFrameType { Data, } +#[derive(Default)] pub struct TelnetTransaction { tx_id: u64, tx_data: AppLayerTxData, } impl TelnetTransaction { - pub fn new() -> TelnetTransaction { - TelnetTransaction { - tx_id: 0, - tx_data: AppLayerTxData::new(), - } + pub fn new() -> Self { + Default::default() } } @@ -92,6 +90,12 @@ impl State for TelnetState { } } +impl Default for TelnetState { + fn default() -> Self { + Self::new() + } +} + impl TelnetState { pub fn new() -> Self { Self {