Skip to content

Commit 8b84150

Browse files
Apply clippy fixes and code improvements (#1890)
* Apply clippy fixes and code improvements - Use clamp() instead of manual min/max patterns - Use while let instead of loop with match - Mark unsafe extern C functions as unsafe - Remove redundant field initialization (speaker: speaker -> speaker) - Use is_multiple_of() instead of modulo comparison - Remove unnecessary mut keyword - Collapse nested if let patterns - Apply various other clippy suggestions Co-Authored-By: unknown <> * Fix: restore mut keyword for platform-specific builder reassignment The builder variable is reassigned in cfg(target_os = macos) and cfg(target_os = windows) blocks, so mut is required. Added #[allow(unused_mut)] to suppress the warning on Linux where these blocks are not compiled. Co-Authored-By: unknown <> --------- Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
1 parent bfb071f commit 8b84150

File tree

13 files changed

+61
-75
lines changed

13 files changed

+61
-75
lines changed

crates/audio-utils/src/lib.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -253,8 +253,7 @@ pub fn chunk_audio_file(
253253
};
254254
let samples_per_chunk = frames_per_chunk
255255
.saturating_mul(channels)
256-
.max(1)
257-
.min(usize::MAX);
256+
.clamp(1, usize::MAX);
258257

259258
let sample_count = samples.len();
260259
let frame_count = sample_count / channels;
@@ -276,5 +275,5 @@ pub fn chunk_size_for_stt(sample_rate: u32) -> usize {
276275
const CHUNK_MS: u32 = 120;
277276

278277
let samples = ((sample_rate as u64) * (CHUNK_MS as u64)) / 1000;
279-
samples.max(1024).min(7168) as usize
278+
samples.clamp(1024, 7168) as usize
280279
}

crates/llama/src/parser.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,8 @@ impl StreamingParser {
3838
self.buffer.push_str(chunk);
3939
let mut responses = Vec::new();
4040

41-
loop {
42-
match self.try_parse_next() {
43-
Some(response) => responses.push(response),
44-
None => break,
45-
}
41+
while let Some(response) = self.try_parse_next() {
42+
responses.push(response);
4643
}
4744

4845
if !self.buffer.is_empty() && !self.looks_like_block_start() {

crates/notification-macos/src/lib.rs

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,23 +33,17 @@ where
3333
}
3434

3535
#[no_mangle]
36-
pub extern "C" fn rust_on_notification_confirm(id_ptr: *const c_char) {
36+
pub unsafe extern "C" fn rust_on_notification_confirm(id_ptr: *const c_char) {
3737
if let Some(cb) = CONFIRM_CB.lock().unwrap().as_ref() {
38-
let id = unsafe { CStr::from_ptr(id_ptr) }
39-
.to_str()
40-
.unwrap()
41-
.to_string();
38+
let id = CStr::from_ptr(id_ptr).to_str().unwrap().to_string();
4239
cb(id);
4340
}
4441
}
4542

4643
#[no_mangle]
47-
pub extern "C" fn rust_on_notification_dismiss(id_ptr: *const c_char) {
44+
pub unsafe extern "C" fn rust_on_notification_dismiss(id_ptr: *const c_char) {
4845
if let Some(cb) = DISMISS_CB.lock().unwrap().as_ref() {
49-
let id = unsafe { CStr::from_ptr(id_ptr) }
50-
.to_str()
51-
.unwrap()
52-
.to_string();
46+
let id = CStr::from_ptr(id_ptr).to_str().unwrap().to_string();
5347
cb(id);
5448
}
5549
}

crates/s3/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ impl Deref for UserClient<'_> {
133133
type Target = Client;
134134

135135
fn deref(&self) -> &Self::Target {
136-
&self.client
136+
self.client
137137
}
138138
}
139139

crates/transcribe-deepgram/src/service.rs

Lines changed: 35 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -111,43 +111,44 @@ impl TranscribeService {
111111
{
112112
Ok(mut deepgram_stream) => {
113113
while let Some(result) = deepgram_stream.next().await {
114-
if let Ok(response) = result {
115-
if let deepgram::common::stream_response::StreamResponse::TranscriptResponse {
116-
channel,
117-
..
118-
} = response {
119-
if let Some(first_alt) = channel.alternatives.first() {
120-
let mut words = Vec::new();
121-
122-
if !first_alt.words.is_empty() {
123-
for word in &first_alt.words {
124-
words.push(Word2 {
125-
text: word.word.clone(),
126-
speaker: None,
127-
confidence: Some(word.confidence as f32),
128-
start_ms: Some((word.start * 1000.0) as u64),
129-
end_ms: Some((word.end * 1000.0) as u64),
130-
});
131-
}
132-
} else if !first_alt.transcript.is_empty() {
133-
for text in first_alt.transcript.split_whitespace() {
134-
words.push(Word2 {
135-
text: text.to_string(),
136-
speaker: None,
137-
confidence: Some(first_alt.confidence as f32),
138-
start_ms: None,
139-
end_ms: None,
140-
});
141-
}
114+
if let Ok(
115+
deepgram::common::stream_response::StreamResponse::TranscriptResponse {
116+
channel,
117+
..
118+
},
119+
) = result
120+
{
121+
if let Some(first_alt) = channel.alternatives.first() {
122+
let mut words = Vec::new();
123+
124+
if !first_alt.words.is_empty() {
125+
for word in &first_alt.words {
126+
words.push(Word2 {
127+
text: word.word.clone(),
128+
speaker: None,
129+
confidence: Some(word.confidence as f32),
130+
start_ms: Some((word.start * 1000.0) as u64),
131+
end_ms: Some((word.end * 1000.0) as u64),
132+
});
133+
}
134+
} else if !first_alt.transcript.is_empty() {
135+
for text in first_alt.transcript.split_whitespace() {
136+
words.push(Word2 {
137+
text: text.to_string(),
138+
speaker: None,
139+
confidence: Some(first_alt.confidence as f32),
140+
start_ms: None,
141+
end_ms: None,
142+
});
142143
}
144+
}
143145

144-
if !words.is_empty() {
145-
let output_chunk = ListenOutputChunk { meta: None, words };
146+
if !words.is_empty() {
147+
let output_chunk = ListenOutputChunk { meta: None, words };
146148

147-
if let Ok(json) = serde_json::to_string(&output_chunk) {
148-
if sender.send(Message::Text(json.into())).await.is_err() {
149-
break;
150-
}
149+
if let Ok(json) = serde_json::to_string(&output_chunk) {
150+
if sender.send(Message::Text(json.into())).await.is_err() {
151+
break;
151152
}
152153
}
153154
}

crates/transcribe-moonshine/src/service/streaming.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -150,10 +150,8 @@ async fn handle_websocket_connection(
150150

151151
let (ws_sender, ws_receiver) = socket.split();
152152

153-
let redemption_time = Duration::from_millis(std::cmp::min(
154-
std::cmp::max(params.redemption_time_ms.unwrap_or(500), 300),
155-
1200,
156-
));
153+
let redemption_time =
154+
Duration::from_millis(params.redemption_time_ms.unwrap_or(500).clamp(300, 1200));
157155

158156
match params.channels {
159157
1 => {
@@ -270,7 +268,7 @@ where
270268
start: start_f64,
271269
end: start_f64 + duration_f64,
272270
confidence,
273-
speaker: speaker,
271+
speaker,
274272
punctuated_word: None,
275273
language: None,
276274
})

crates/transcribe-whisper-local/src/service/streaming.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ async fn process_transcription_stream(
262262
start: adjusted_start_f64,
263263
end: adjusted_end_f64,
264264
confidence,
265-
speaker: speaker,
265+
speaker,
266266
punctuated_word: None,
267267
language: None,
268268
})

crates/vad3/src/lib.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ pub const FRAME_20MS: usize = 320;
55
pub const FRAME_30MS: usize = 480;
66

77
pub fn choose_optimal_frame_size(len: usize) -> usize {
8-
if len >= FRAME_30MS && len % FRAME_30MS == 0 {
8+
if len >= FRAME_30MS && len.is_multiple_of(FRAME_30MS) {
99
FRAME_30MS
10-
} else if len >= FRAME_20MS && len % FRAME_20MS == 0 {
10+
} else if len >= FRAME_20MS && len.is_multiple_of(FRAME_20MS) {
1111
FRAME_20MS
12-
} else if len >= FRAME_10MS && len % FRAME_10MS == 0 {
12+
} else if len >= FRAME_10MS && len.is_multiple_of(FRAME_10MS) {
1313
FRAME_10MS
1414
} else {
1515
let padding_30 = (FRAME_30MS - (len % FRAME_30MS)) % FRAME_30MS;

owhisper/owhisper-server/src/commands/run/realtime.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -148,14 +148,14 @@ async fn run_audio_stream_with_stop(
148148
data.update(rms);
149149
}
150150

151-
owhisper_interface::MixedMessage::Audio(
152-
hypr_audio_utils::f32_to_i16_bytes(samples.into_iter()).into(),
153-
)
151+
owhisper_interface::MixedMessage::Audio(hypr_audio_utils::f32_to_i16_bytes(
152+
samples.into_iter(),
153+
))
154154
})
155155
};
156156

157157
let client = owhisper_client::ListenClient::builder()
158-
.api_base(&format!("ws://127.0.0.1:{}/v1", port))
158+
.api_base(format!("ws://127.0.0.1:{}/v1", port))
159159
.api_key(api_key.as_deref().unwrap_or(""))
160160
.params(owhisper_interface::ListenParams {
161161
model: Some(model),

owhisper/owhisper-server/src/commands/run/state.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ impl Drop for RunState {
132132
let data_dir = owhisper_config::data_dir();
133133
let session_dir = data_dir.join(self.session_timestamp.to_string());
134134

135-
if let Err(_) = std::fs::create_dir_all(&session_dir) {
135+
if std::fs::create_dir_all(&session_dir).is_err() {
136136
return;
137137
}
138138

0 commit comments

Comments
 (0)