Skip to content

Commit e805501

Browse files
authored
fix clippy warnings (#150)
1 parent 665d1a4 commit e805501

File tree

12 files changed

+154
-134
lines changed

12 files changed

+154
-134
lines changed

Cargo.lock

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ json = "0.12.4"
6767
tokio = { version = "1.18", features = ["full"] }
6868
pretty_env_logger = "0.4.0"
6969
assert_matches = "1"
70+
claims = "0.7.1"
7071
pyroscope_pprofrs = { path = "pyroscope_backends/pyroscope_pprofrs" }
7172
pyroscope_pyspy = { path = "pyroscope_backends/pyroscope_pyspy" }
7273
pyroscope_rbspy = { path = "pyroscope_backends/pyroscope_rbspy" }

examples/transform.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,9 @@ pub fn transform_report(report: Report) -> Report {
4949
})
5050
.collect();
5151

52-
let new_report = Report::new(data).metadata(report.metadata.clone());
53-
54-
new_report
52+
Report::new(data).metadata(report.metadata.clone())
5553
}
54+
5655
fn main() -> Result<()> {
5756
let agent = PyroscopeAgent::builder("http://localhost:4040", "example.transform")
5857
.backend(pprof_backend(PprofConfig::new().sample_rate(100)))

src/backend/tests.rs

Lines changed: 36 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -21,23 +21,24 @@ fn test_stack_frame_display() {
2121

2222
#[test]
2323
fn test_stack_trace_display() {
24-
let mut frames = Vec::new();
25-
frames.push(StackFrame::new(
26-
Some("module".to_string()),
27-
Some("name".to_string()),
28-
Some("filename".to_string()),
29-
Some("relative_path".to_string()),
30-
Some("absolute_path".to_string()),
31-
Some(1),
32-
));
33-
frames.push(StackFrame::new(
34-
Some("module".to_string()),
35-
Some("name".to_string()),
36-
Some("filename".to_string()),
37-
Some("relative_path".to_string()),
38-
Some("absolute_path".to_string()),
39-
Some(2),
40-
));
24+
let frames = vec![
25+
StackFrame::new(
26+
Some("module".to_string()),
27+
Some("name".to_string()),
28+
Some("filename".to_string()),
29+
Some("relative_path".to_string()),
30+
Some("absolute_path".to_string()),
31+
Some(1),
32+
),
33+
StackFrame::new(
34+
Some("module".to_string()),
35+
Some("name".to_string()),
36+
Some("filename".to_string()),
37+
Some("relative_path".to_string()),
38+
Some("absolute_path".to_string()),
39+
Some(2),
40+
),
41+
];
4142

4243
let stack_trace = StackTrace::new(&BackendConfig::default(), None, None, None, frames);
4344

@@ -73,23 +74,24 @@ fn test_report_clear() {
7374
#[test]
7475
fn test_report_display() {
7576
// Dummy StackTrace
76-
let mut frames = Vec::new();
77-
frames.push(StackFrame::new(
78-
Some("module".to_string()),
79-
Some("name".to_string()),
80-
Some("filename".to_string()),
81-
Some("absolute_path".to_string()),
82-
Some("relative_path".to_string()),
83-
Some(1),
84-
));
85-
frames.push(StackFrame::new(
86-
Some("module".to_string()),
87-
Some("name".to_string()),
88-
Some("filename".to_string()),
89-
Some("absolute_path".to_string()),
90-
Some("relative_path".to_string()),
91-
Some(2),
92-
));
77+
let frames = vec![
78+
StackFrame::new(
79+
Some("module".to_string()),
80+
Some("name".to_string()),
81+
Some("filename".to_string()),
82+
Some("absolute_path".to_string()),
83+
Some("relative_path".to_string()),
84+
Some(1),
85+
),
86+
StackFrame::new(
87+
Some("module".to_string()),
88+
Some("name".to_string()),
89+
Some("filename".to_string()),
90+
Some("absolute_path".to_string()),
91+
Some("relative_path".to_string()),
92+
Some(2),
93+
),
94+
];
9395

9496
let stack_trace = StackTrace::new(&BackendConfig::default(), None, None, None, frames);
9597

src/backend/types.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,7 @@ impl From<StackBuffer> for Vec<Report> {
9393
},
9494
)
9595
.unwrap_or_default()
96-
.into_iter()
97-
.map(|(_, report)| report)
96+
.into_values()
9897
.collect()
9998
}
10099
}

src/encode/folded.rs

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
1-
21
use crate::backend;
32

4-
use backend::types::{Report, EncodedReport};
3+
use backend::types::{EncodedReport, Report};
54

6-
pub fn encode(reports: &Vec<Report>) -> Vec<EncodedReport> {
7-
reports.into_iter()
8-
.map(|r| {
9-
EncodedReport {
10-
format: "folded".to_string(),
11-
content_type: "binary/octet-stream".to_string(),
12-
content_encoding: "".to_string(),
13-
data: r.to_string().into_bytes(),
14-
metadata: r.metadata.to_owned(),
15-
}
16-
}).collect()
17-
}
5+
pub fn encode(reports: &[Report]) -> Vec<EncodedReport> {
6+
reports
7+
.iter()
8+
.map(|r| EncodedReport {
9+
format: "folded".to_string(),
10+
content_type: "binary/octet-stream".to_string(),
11+
content_encoding: "".to_string(),
12+
data: r.to_string().into_bytes(),
13+
metadata: r.metadata.to_owned(),
14+
})
15+
.collect()
16+
}

src/encode/pprof.rs

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ use prost::Message;
55
use crate::backend::types::{EncodedReport, Report};
66
use crate::encode::profiles::{Function, Label, Line, Location, Profile, Sample, ValueType};
77

8-
98
struct PProfBuilder {
109
profile: Profile,
1110
strings: HashMap<String, i64>,
@@ -28,8 +27,8 @@ pub struct FunctionMirror {
2827
impl PProfBuilder {
2928
fn add_string(&mut self, s: &String) -> i64 {
3029
let v = self.strings.get(s);
31-
if v.is_some() {
32-
return *v.unwrap();
30+
if let Some(v) = v {
31+
return *v;
3332
}
3433
assert!(self.strings.len() != self.profile.string_table.len() + 1);
3534
let id: i64 = self.strings.len() as i64;
@@ -40,13 +39,13 @@ impl PProfBuilder {
4039

4140
fn add_function(&mut self, fm: FunctionMirror) -> u64 {
4241
let v = self.functions.get(&fm);
43-
if v.is_some() {
44-
return *v.unwrap();
42+
if let Some(v) = v {
43+
return *v;
4544
}
4645
assert!(self.functions.len() != self.profile.function.len() + 1);
4746
let id: u64 = self.functions.len() as u64 + 1;
4847
let f = Function {
49-
id: id,
48+
id,
5049
name: fm.name,
5150
system_name: 0,
5251
filename: fm.filename,
@@ -59,8 +58,8 @@ impl PProfBuilder {
5958

6059
fn add_location(&mut self, lm: LocationMirror) -> u64 {
6160
let v = self.locations.get(&lm);
62-
if v.is_some() {
63-
return *v.unwrap();
61+
if let Some(v) = v {
62+
return *v;
6463
}
6564
assert!(self.locations.len() != self.profile.location.len() + 1);
6665
let id: u64 = self.locations.len() as u64 + 1;
@@ -80,7 +79,9 @@ impl PProfBuilder {
8079
}
8180
}
8281

83-
pub fn encode(reports: &Vec<Report>, sample_rate: u32, start_time_nanos: u64, duration_nanos: u64) -> Vec<EncodedReport> {
82+
pub fn encode(
83+
reports: &Vec<Report>, sample_rate: u32, start_time_nanos: u64, duration_nanos: u64,
84+
) -> Vec<EncodedReport> {
8485
let mut b = PProfBuilder {
8586
strings: HashMap::new(),
8687
functions: HashMap::new(),
@@ -125,18 +126,12 @@ pub fn encode(reports: &Vec<Report>, sample_rate: u32, start_time_nanos: u64, du
125126
label: vec![],
126127
};
127128
for sf in &stacktrace.frames {
128-
let name = b.add_string(&sf.name.as_ref().unwrap_or(&"".to_string()));
129-
let filename = b.add_string(&sf.filename.as_ref().unwrap_or(&"".to_string()));
129+
let name = b.add_string(sf.name.as_ref().unwrap_or(&"".to_string()));
130+
let filename = b.add_string(sf.filename.as_ref().unwrap_or(&"".to_string()));
130131
let line = sf.line.unwrap_or(0) as i64;
131-
let function_id = b.add_function(FunctionMirror {
132-
name: name,
133-
filename: filename,
134-
});
135-
let location_id = b.add_location(LocationMirror {
136-
function_id: function_id,
137-
line: line,
138-
});
139-
sample.location_id.push(location_id as u64);
132+
let function_id = b.add_function(FunctionMirror { name, filename });
133+
let location_id = b.add_location(LocationMirror { function_id, line });
134+
sample.location_id.push(location_id);
140135
}
141136
let mut labels = HashMap::new();
142137
for l in &stacktrace.metadata.tags {

src/pyroscope.rs

Lines changed: 31 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use json;
2222

2323
use crate::backend::BackendImpl;
2424
use crate::pyroscope::Compression::GZIP;
25-
use crate::pyroscope::ReportEncoding::{PPROF};
25+
use crate::pyroscope::ReportEncoding::PPROF;
2626

2727
const LOG_TAG: &str = "Pyroscope::Agent";
2828

@@ -102,7 +102,7 @@ impl PyroscopeConfig {
102102
spy_name: String::from("undefined"), // Spy Name should be set by the backend
103103
auth_token: None, // No authentication token
104104
basic_auth: None,
105-
func: None, // No function
105+
func: None, // No function
106106
compression: None,
107107
report_encoding: ReportEncoding::FOLDED,
108108
tenant_id: None,
@@ -139,7 +139,6 @@ impl PyroscopeConfig {
139139
Self { spy_name, ..self }
140140
}
141141

142-
143142
pub fn auth_token(self, auth_token: String) -> Self {
144143
Self {
145144
auth_token: Some(auth_token),
@@ -185,7 +184,6 @@ impl PyroscopeConfig {
185184
}
186185
}
187186

188-
189187
/// Set the http request body compression.
190188
///
191189
/// # Example
@@ -203,7 +201,7 @@ impl PyroscopeConfig {
203201

204202
pub fn report_encoding(self, report_encoding: ReportEncoding) -> Self {
205203
Self {
206-
report_encoding: report_encoding,
204+
report_encoding,
207205
..self
208206
}
209207
}
@@ -217,7 +215,7 @@ impl PyroscopeConfig {
217215

218216
pub fn http_headers(self, http_headers: HashMap<String, String>) -> Self {
219217
Self {
220-
http_headers: http_headers,
218+
http_headers,
221219
..self
222220
}
223221
}
@@ -328,7 +326,9 @@ impl PyroscopeAgentBuilder {
328326

329327
pub fn basic_auth(self, username: impl AsRef<str>, password: impl AsRef<str>) -> Self {
330328
Self {
331-
config: self.config.basic_auth(username.as_ref().to_owned(), password.as_ref().to_owned()),
329+
config: self
330+
.config
331+
.basic_auth(username.as_ref().to_owned(), password.as_ref().to_owned()),
332332
..self
333333
}
334334
}
@@ -455,7 +455,7 @@ impl PyroscopeAgentBuilder {
455455
handle: None,
456456
running: Arc::new((
457457
#[allow(clippy::mutex_atomic)]
458-
Mutex::new(false),
458+
Mutex::new(false),
459459
Condvar::new(),
460460
)),
461461
_state: PhantomData,
@@ -465,7 +465,7 @@ impl PyroscopeAgentBuilder {
465465

466466
#[derive(Clone, Debug)]
467467
pub enum Compression {
468-
GZIP
468+
GZIP,
469469
}
470470

471471
impl FromStr for Compression {
@@ -843,31 +843,42 @@ pub fn parse_http_headers_json(http_headers_json: String) -> Result<HashMap<Stri
843843
let mut http_headers = HashMap::new();
844844
let parsed = json::parse(&http_headers_json)?;
845845
if !parsed.is_object() {
846-
return Err(PyroscopeError::AdHoc(format!("expected object, got {}", parsed)));
846+
return Err(PyroscopeError::AdHoc(format!(
847+
"expected object, got {}",
848+
parsed
849+
)));
847850
}
848851
for (k, v) in parsed.entries() {
849852
if v.is_string() {
850853
http_headers.insert(k.to_string(), v.to_string());
851854
} else {
852-
return Err(PyroscopeError::AdHoc(format!("invalid http header value, not a string: {}", v.to_string())));
855+
return Err(PyroscopeError::AdHoc(format!(
856+
"invalid http header value, not a string: {}",
857+
v
858+
)));
853859
}
854-
};
855-
return Ok(http_headers);
860+
}
861+
Ok(http_headers)
856862
}
857863

858864
pub fn parse_vec_string_json(s: String) -> Result<Vec<String>> {
859865
let parsed = json::parse(&s)?;
860866
if !parsed.is_array() {
861-
return Err(PyroscopeError::AdHoc(format!("expected array, got {}", parsed)));
867+
return Err(PyroscopeError::AdHoc(format!(
868+
"expected array, got {}",
869+
parsed
870+
)));
862871
}
863-
let mut res = Vec::new();
864-
res.reserve(parsed.len());
872+
let mut res = Vec::with_capacity(parsed.len());
865873
for v in parsed.members() {
866874
if v.is_string() {
867875
res.push(v.to_string());
868876
} else {
869-
return Err(PyroscopeError::AdHoc(format!("invalid element value, not a string: {}", v.to_string())));
877+
return Err(PyroscopeError::AdHoc(format!(
878+
"invalid element value, not a string: {}",
879+
v
880+
)));
870881
}
871-
};
872-
return Ok(res);
873-
}
882+
}
883+
Ok(res)
884+
}

0 commit comments

Comments
 (0)