Skip to content

Commit 45b6b56

Browse files
committed
Collapse a few nested if now that we have if let chains
1 parent 7599e47 commit 45b6b56

File tree

18 files changed

+204
-208
lines changed

18 files changed

+204
-208
lines changed

crates/cli/build.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@ fn main() -> anyhow::Result<()> {
1212
// At build time, we override the version through the environment variable
1313
// VERGEN_GIT_DESCRIBE. In some contexts, it means this variable is set but
1414
// empty, so we unset it here.
15-
if let Ok(ver) = std::env::var("VERGEN_GIT_DESCRIBE") {
16-
if ver.is_empty() {
17-
#[allow(unsafe_code)]
18-
// SAFETY: This is safe because the build script is running a single thread
19-
unsafe {
20-
std::env::remove_var("VERGEN_GIT_DESCRIBE");
21-
}
15+
if let Ok(ver) = std::env::var("VERGEN_GIT_DESCRIBE")
16+
&& ver.is_empty()
17+
{
18+
#[allow(unsafe_code)]
19+
// SAFETY: This is safe because the build script is running a single thread
20+
unsafe {
21+
std::env::remove_var("VERGEN_GIT_DESCRIBE");
2222
}
2323
}
2424

crates/cli/src/app_state.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -275,10 +275,10 @@ fn infer_client_ip(
275275

276276
let peer = if let Some(info) = connection_info {
277277
// We can always trust the proxy protocol to give us the correct IP address
278-
if let Some(proxy) = info.get_proxy_ref() {
279-
if let Some(source) = proxy.source() {
280-
return Some(source.ip());
281-
}
278+
if let Some(proxy) = info.get_proxy_ref()
279+
&& let Some(source) = proxy.source()
280+
{
281+
return Some(source.ip());
282282
}
283283

284284
info.get_peer_addr().map(|addr| addr.ip())

crates/cli/src/commands/manage.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -619,13 +619,12 @@ impl Options {
619619
let txn = conn.begin().await?;
620620
let mut repo = PgRepository::from_conn(txn);
621621

622-
if let Some(password) = &password {
623-
if !ignore_password_complexity
624-
&& !password_manager.is_password_complex_enough(password)?
625-
{
626-
error!("That password is too weak.");
627-
return Ok(ExitCode::from(1));
628-
}
622+
if let Some(password) = &password
623+
&& !ignore_password_complexity
624+
&& !password_manager.is_password_complex_enough(password)?
625+
{
626+
error!("That password is too weak.");
627+
return Ok(ExitCode::from(1));
629628
}
630629

631630
// If the username is provided, check if it's available and normalize it.

crates/cli/src/sync.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -208,11 +208,11 @@ pub async fn config_sync(
208208
// private key to hold the content of the private key file.
209209
// private key (raw) takes precedence so both can be defined
210210
// without issues
211-
if siwa.private_key.is_none() {
212-
if let Some(private_key_file) = siwa.private_key_file.take() {
213-
let key = tokio::fs::read_to_string(private_key_file).await?;
214-
siwa.private_key = Some(key);
215-
}
211+
if siwa.private_key.is_none()
212+
&& let Some(private_key_file) = siwa.private_key_file.take()
213+
{
214+
let key = tokio::fs::read_to_string(private_key_file).await?;
215+
siwa.private_key = Some(key);
216216
}
217217
let encoded = serde_json::to_vec(&siwa)?;
218218
Some(encrypter.encrypt_to_string(&encoded)?)

crates/config/src/sections/telemetry.rs

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -198,34 +198,34 @@ impl ConfigurationSection for TelemetryConfig {
198198
&self,
199199
_figment: &figment::Figment,
200200
) -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>> {
201-
if let Some(sample_rate) = self.sentry.sample_rate {
202-
if !(0.0..=1.0).contains(&sample_rate) {
203-
return Err(figment::error::Error::custom(
204-
"Sentry sample rate must be between 0.0 and 1.0",
205-
)
206-
.with_path("sentry.sample_rate")
207-
.into());
208-
}
201+
if let Some(sample_rate) = self.sentry.sample_rate
202+
&& !(0.0..=1.0).contains(&sample_rate)
203+
{
204+
return Err(figment::error::Error::custom(
205+
"Sentry sample rate must be between 0.0 and 1.0",
206+
)
207+
.with_path("sentry.sample_rate")
208+
.into());
209209
}
210210

211-
if let Some(sample_rate) = self.sentry.traces_sample_rate {
212-
if !(0.0..=1.0).contains(&sample_rate) {
213-
return Err(figment::error::Error::custom(
214-
"Sentry sample rate must be between 0.0 and 1.0",
215-
)
216-
.with_path("sentry.traces_sample_rate")
217-
.into());
218-
}
211+
if let Some(sample_rate) = self.sentry.traces_sample_rate
212+
&& !(0.0..=1.0).contains(&sample_rate)
213+
{
214+
return Err(figment::error::Error::custom(
215+
"Sentry sample rate must be between 0.0 and 1.0",
216+
)
217+
.with_path("sentry.traces_sample_rate")
218+
.into());
219219
}
220220

221-
if let Some(sample_rate) = self.tracing.sample_rate {
222-
if !(0.0..=1.0).contains(&sample_rate) {
223-
return Err(figment::error::Error::custom(
224-
"Tracing sample rate must be between 0.0 and 1.0",
225-
)
226-
.with_path("tracing.sample_rate")
227-
.into());
228-
}
221+
if let Some(sample_rate) = self.tracing.sample_rate
222+
&& !(0.0..=1.0).contains(&sample_rate)
223+
{
224+
return Err(figment::error::Error::custom(
225+
"Tracing sample rate must be between 0.0 and 1.0",
226+
)
227+
.with_path("tracing.sample_rate")
228+
.into());
229229
}
230230

231231
Ok(())

crates/context/src/fmt.rs

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -129,31 +129,31 @@ where
129129
field_fromatter.format_fields(writer.by_ref(), event)?;
130130

131131
// If we have a OTEL span, we can add the trace ID to the end of the log line
132-
if let Some(span) = ctx.lookup_current() {
133-
if let Some(otel) = span.extensions().get::<OtelData>() {
134-
let parent_cx_span = otel.parent_cx.span();
135-
let sc = parent_cx_span.span_context();
136-
137-
// Check if the span is sampled, first from the span builder,
138-
// then from the parent context if nothing is set there
139-
if otel
140-
.builder
141-
.sampling_result
142-
.as_ref()
143-
.map_or(sc.is_sampled(), |r| {
144-
r.decision == SamplingDecision::RecordAndSample
145-
})
146-
{
147-
// If it is the root span, the trace ID will be in the span builder. Else, it
148-
// will be in the parent OTEL context
149-
let trace_id = otel.builder.trace_id.unwrap_or(sc.trace_id());
150-
if trace_id != TraceId::INVALID {
151-
let label = Style::new()
152-
.italic()
153-
.force_styling(ansi)
154-
.apply_to("trace.id");
155-
write!(&mut writer, " {label}={trace_id}")?;
156-
}
132+
if let Some(span) = ctx.lookup_current()
133+
&& let Some(otel) = span.extensions().get::<OtelData>()
134+
{
135+
let parent_cx_span = otel.parent_cx.span();
136+
let sc = parent_cx_span.span_context();
137+
138+
// Check if the span is sampled, first from the span builder,
139+
// then from the parent context if nothing is set there
140+
if otel
141+
.builder
142+
.sampling_result
143+
.as_ref()
144+
.map_or(sc.is_sampled(), |r| {
145+
r.decision == SamplingDecision::RecordAndSample
146+
})
147+
{
148+
// If it is the root span, the trace ID will be in the span builder. Else, it
149+
// will be in the parent OTEL context
150+
let trace_id = otel.builder.trace_id.unwrap_or(sc.trace_id());
151+
if trace_id != TraceId::INVALID {
152+
let label = Style::new()
153+
.italic()
154+
.force_styling(ansi)
155+
.apply_to("trace.id");
156+
write!(&mut writer, " {label}={trace_id}")?;
157157
}
158158
}
159159
}

crates/data-model/src/user_agent.rs

Lines changed: 28 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -88,32 +88,31 @@ impl UserAgent {
8888

8989
#[must_use]
9090
pub fn parse(user_agent: String) -> Self {
91-
if !user_agent.contains("Mozilla/") {
92-
if let Some((name, version, model, os, os_version)) =
91+
if !user_agent.contains("Mozilla/")
92+
&& let Some((name, version, model, os, os_version)) =
9393
UserAgent::parse_custom(&user_agent)
94-
{
95-
let mut device_type = DeviceType::Unknown;
96-
97-
// Handle mobile simple mobile devices
98-
if os == "Android" || os == "iOS" {
99-
device_type = DeviceType::Mobile;
100-
}
101-
102-
// Handle iPads
103-
if model.contains("iPad") {
104-
device_type = DeviceType::Tablet;
105-
}
106-
107-
return Self {
108-
name: Some(name.to_owned()),
109-
version: Some(version.to_owned()),
110-
os: Some(os.to_owned()),
111-
os_version: os_version.map(std::borrow::ToOwned::to_owned),
112-
model: Some(model.to_owned()),
113-
device_type,
114-
raw: user_agent,
115-
};
94+
{
95+
let mut device_type = DeviceType::Unknown;
96+
97+
// Handle mobile simple mobile devices
98+
if os == "Android" || os == "iOS" {
99+
device_type = DeviceType::Mobile;
100+
}
101+
102+
// Handle iPads
103+
if model.contains("iPad") {
104+
device_type = DeviceType::Tablet;
116105
}
106+
107+
return Self {
108+
name: Some(name.to_owned()),
109+
version: Some(version.to_owned()),
110+
os: Some(os.to_owned()),
111+
os_version: os_version.map(std::borrow::ToOwned::to_owned),
112+
model: Some(model.to_owned()),
113+
device_type,
114+
raw: user_agent,
115+
};
117116
}
118117

119118
let mut model = None;
@@ -205,11 +204,11 @@ impl UserAgent {
205204
}
206205

207206
// Special handling for Electron applications e.g. Element Desktop
208-
if user_agent.contains("Electron/") {
209-
if let Some(app) = UserAgent::parse_electron(&user_agent) {
210-
result.name = app.0;
211-
result.version = app.1;
212-
}
207+
if user_agent.contains("Electron/")
208+
&& let Some(app) = UserAgent::parse_electron(&user_agent)
209+
{
210+
result.name = app.0;
211+
result.version = app.1;
213212
}
214213

215214
Self {

crates/data-model/src/users.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -223,17 +223,17 @@ impl UserRegistrationToken {
223223
}
224224

225225
// Check if expired
226-
if let Some(expires_at) = self.expires_at {
227-
if now >= expires_at {
228-
return false;
229-
}
226+
if let Some(expires_at) = self.expires_at
227+
&& now >= expires_at
228+
{
229+
return false;
230230
}
231231

232232
// Check if usage limit exceeded
233-
if let Some(usage_limit) = self.usage_limit {
234-
if self.times_used >= usage_limit {
235-
return false;
236-
}
233+
if let Some(usage_limit) = self.usage_limit
234+
&& self.times_used >= usage_limit
235+
{
236+
return false;
237237
}
238238

239239
true

crates/handlers/src/admin/call_context.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -187,10 +187,10 @@ where
187187
};
188188

189189
// If there is a user for this session, check that it is not locked
190-
if let Some(user) = &user {
191-
if !user.is_valid() {
192-
return Err(Rejection::UserLocked);
193-
}
190+
if let Some(user) = &user
191+
&& !user.is_valid()
192+
{
193+
return Err(Rejection::UserLocked);
194194
}
195195

196196
if !session.is_valid() {

crates/handlers/src/lib.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -475,13 +475,13 @@ fn recover_error(
475475
) -> axum::response::Response {
476476
// Error responses should have an ErrorContext attached to them
477477
let ext = response.extensions().get::<ErrorContext>();
478-
if let Some(ctx) = ext {
479-
if let Ok(res) = templates.render_error(ctx) {
480-
let (mut parts, _original_body) = response.into_parts();
481-
parts.headers.remove(CONTENT_TYPE);
482-
parts.headers.remove(CONTENT_LENGTH);
483-
return (parts, Html(res)).into_response();
484-
}
478+
if let Some(ctx) = ext
479+
&& let Ok(res) = templates.render_error(ctx)
480+
{
481+
let (mut parts, _original_body) = response.into_parts();
482+
parts.headers.remove(CONTENT_TYPE);
483+
parts.headers.remove(CONTENT_LENGTH);
484+
return (parts, Html(res)).into_response();
485485
}
486486

487487
response

0 commit comments

Comments
 (0)