Skip to content

Commit a69ea8f

Browse files
committed
Forbid usage of some complex Option:: methods.
1 parent c7d3314 commit a69ea8f

File tree

7 files changed

+58
-45
lines changed

7 files changed

+58
-45
lines changed

quickwit/clippy.toml

+12-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,17 @@
11
disallowed-methods = [
2-
"std::path::Path::exists"
2+
# This function is not sound because it does not return a Result
3+
"std::path::Path::exists",
4+
# These functions hurt readability (according to Paul)
5+
"std::option::Option::is_some_and",
6+
"std::option::Option::is_none_or",
7+
"std::option::Option::xor",
8+
# "std::option::Option::and_then",
9+
# .map(..).unwrap_or(..) or let Some(..) else {..}
10+
"std::option::Option::map_or",
11+
# .map(..).unwrap_or_else(..) or let Some(..) else {..}
12+
"std::option::Option::map_or_else",
313
]
14+
415
ignore-interior-mutability = [
516
"bytes::Bytes",
617
"bytestring::ByteString",

quickwit/quickwit-common/src/uri.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414

1515
use std::borrow::Cow;
1616
use std::env;
17-
use std::ffi::OsStr;
1817
use std::fmt::{Debug, Display};
1918
use std::hash::Hash;
2019
use std::path::{Component, Path, PathBuf};
@@ -126,7 +125,7 @@ impl Uri {
126125

127126
/// Returns the extension of the URI.
128127
pub fn extension(&self) -> Option<&str> {
129-
Path::new(&self.uri).extension().and_then(OsStr::to_str)
128+
Path::new(&self.uri).extension()?.to_str()
130129
}
131130

132131
/// Returns the URI as a string slice.

quickwit/quickwit-ingest/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ mod doc_batch;
1818
pub mod error;
1919
mod ingest_api_service;
2020
#[path = "codegen/ingest_service.rs"]
21+
#[allow(clippy::disallowed_methods)]
2122
mod ingest_service;
2223
mod ingest_v2;
2324
mod memory_capacity;

quickwit/quickwit-janitor/src/janitor_service.rs

+6-4
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,13 @@ impl JanitorService {
4040
}
4141

4242
fn is_healthy(&self) -> bool {
43-
self.delete_task_service_handle
44-
.as_ref()
45-
.is_none_or(|delete_task_service_handle| {
43+
let delete_task_is_not_failure: bool =
44+
if let Some(delete_task_service_handle) = &self.delete_task_service_handle {
4645
delete_task_service_handle.state() != ActorState::Failure
47-
})
46+
} else {
47+
true
48+
};
49+
delete_task_is_not_failure
4850
&& self.garbage_collector_handle.state() != ActorState::Failure
4951
&& self.retention_policy_executor_handle.state() != ActorState::Failure
5052
}

quickwit/quickwit-proto/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
// limitations under the License.
1414

1515
#![allow(clippy::derive_partial_eq_without_eq)]
16-
#![deny(clippy::disallowed_methods)]
16+
#![allow(clippy::disallowed_methods)]
1717
#![allow(rustdoc::invalid_html_tags)]
1818

1919
use std::cmp::Ordering;

quickwit/quickwit-search/src/leaf_cache.rs

+32-29
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
use std::ops::Bound;
15+
use std::ops::{Bound, RangeBounds};
1616

1717
use prost::Message;
1818
use quickwit_proto::search::{
@@ -83,16 +83,16 @@ struct CacheKey {
8383
request: SearchRequest,
8484
/// The effective time range of the request, that is, the intersection of the timerange
8585
/// requested, and the timerange covered by the split.
86-
merged_time_range: Range,
86+
merged_time_range: HalfOpenRange,
8787
}
8888

8989
impl CacheKey {
9090
fn from_split_meta_and_request(
9191
split_info: SplitIdAndFooterOffsets,
9292
mut search_request: SearchRequest,
9393
) -> Self {
94-
let split_time_range = Range::from_bounds(split_info.time_range());
95-
let request_time_range = Range::from_bounds(search_request.time_range());
94+
let split_time_range = HalfOpenRange::from_bounds(split_info.time_range());
95+
let request_time_range = HalfOpenRange::from_bounds(search_request.time_range());
9696
let merged_time_range = request_time_range.intersect(&split_time_range);
9797

9898
search_request.start_timestamp = None;
@@ -110,28 +110,30 @@ impl CacheKey {
110110
}
111111

112112
/// A (half-open) range bounded inclusively below and exclusively above [start..end).
113-
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
114-
struct Range {
113+
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
114+
struct HalfOpenRange {
115115
start: i64,
116116
end: Option<i64>,
117117
}
118118

119-
impl Range {
120-
/// Create a Range from bounds.
121-
fn from_bounds(range: impl std::ops::RangeBounds<i64>) -> Self {
122-
let empty_range = Range {
119+
impl HalfOpenRange {
120+
fn empty_range() -> HalfOpenRange {
121+
HalfOpenRange {
123122
start: 0,
124123
end: Some(0),
125-
};
124+
}
125+
}
126126

127+
/// Create a Range from bounds.
128+
fn from_bounds(range: impl RangeBounds<i64>) -> Self {
127129
let start = match range.start_bound() {
128130
Bound::Included(start) => *start,
129131
Bound::Excluded(start) => {
130132
// if we exclude i64::MAX from the start bound, the range is necessarily empty
131133
if let Some(start) = start.checked_add(1) {
132134
start
133135
} else {
134-
return empty_range;
136+
return Self::empty_range();
135137
}
136138
}
137139
Bound::Unbounded => i64::MIN,
@@ -143,44 +145,45 @@ impl Range {
143145
Bound::Unbounded => None,
144146
};
145147

146-
Range { start, end }
148+
HalfOpenRange { start, end }.normalize()
149+
}
150+
151+
fn is_empty(self) -> bool {
152+
!self.contains(&self.start)
147153
}
148154

149155
/// Normalize empty ranges to be 0..0
150-
fn normalize(self) -> Range {
151-
let empty_range = Range {
152-
start: 0,
153-
end: Some(0),
154-
};
155-
match self {
156-
Range {
157-
start,
158-
end: Some(end),
159-
} if start >= end => empty_range,
160-
any => any,
156+
fn normalize(self) -> HalfOpenRange {
157+
if self.is_empty() {
158+
Self::empty_range()
159+
} else {
160+
self
161161
}
162162
}
163163

164164
/// Return the intersection of self and other.
165-
fn intersect(&self, other: &Range) -> Range {
165+
fn intersect(&self, other: &HalfOpenRange) -> HalfOpenRange {
166166
let start = self.start.max(other.start);
167-
168167
let end = match (self.end, other.end) {
169168
(Some(this), Some(other)) => Some(this.min(other)),
170169
(Some(this), None) => Some(this),
171170
(None, other) => other,
172171
};
173-
Range { start, end }.normalize()
172+
HalfOpenRange { start, end }.normalize()
174173
}
175174
}
176175

177-
impl std::ops::RangeBounds<i64> for Range {
176+
impl RangeBounds<i64> for HalfOpenRange {
178177
fn start_bound(&self) -> Bound<&i64> {
179178
Bound::Included(&self.start)
180179
}
181180

182181
fn end_bound(&self) -> Bound<&i64> {
183-
self.end.as_ref().map_or(Bound::Unbounded, Bound::Excluded)
182+
if let Some(end_bound) = &self.end {
183+
Bound::Excluded(end_bound)
184+
} else {
185+
Bound::Unbounded
186+
}
184187
}
185188
}
186189

quickwit/quickwit-search/src/root.rs

+5-8
Original file line numberDiff line numberDiff line change
@@ -1519,10 +1519,8 @@ impl ExtractTimestampRange<'_> {
15191519
// a match_none, but the visitor doesn't allow mutation.
15201520
lower_bound = lower_bound.saturating_add(1);
15211521
}
1522-
self.start_timestamp = Some(
1523-
self.start_timestamp
1524-
.map_or(lower_bound, |current| current.max(lower_bound)),
1525-
);
1522+
1523+
self.start_timestamp = self.start_timestamp.max(Some(lower_bound));
15261524
}
15271525

15281526
fn update_end_timestamp(&mut self, upper_bound: &quickwit_query::JsonLiteral, included: bool) {
@@ -1537,10 +1535,9 @@ impl ExtractTimestampRange<'_> {
15371535
// a match_none, but the visitor doesn't allow mutation.
15381536
upper_bound = upper_bound.saturating_add(1);
15391537
}
1540-
self.end_timestamp = Some(
1541-
self.end_timestamp
1542-
.map_or(upper_bound, |current| current.min(upper_bound)),
1543-
);
1538+
1539+
let new_end_timestamp = self.end_timestamp.unwrap_or(upper_bound).min(upper_bound);
1540+
self.end_timestamp = Some(new_end_timestamp);
15441541
}
15451542
}
15461543

0 commit comments

Comments
 (0)