Skip to content

Commit aaf9f7d

Browse files
authored
fix some clippy warnings (#6695)
1 parent e1b93b8 commit aaf9f7d

File tree

5 files changed

+95
-139
lines changed

5 files changed

+95
-139
lines changed

packages/libraries/router/src/agent.rs

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ pub struct ExecutionReport {
7575
pub persisted_document_hash: Option<String>,
7676
}
7777

78-
#[derive(Debug, Clone)]
78+
#[derive(Debug, Clone, Default)]
7979
pub struct State {
8080
buffer: VecDeque<ExecutionReport>,
8181
schema: Document<'static, String>,
@@ -99,15 +99,6 @@ impl State {
9999
}
100100
}
101101

102-
impl Default for State {
103-
fn default() -> Self {
104-
Self {
105-
buffer: VecDeque::new(),
106-
schema: Default::default(),
107-
}
108-
}
109-
}
110-
111102
#[derive(Clone)]
112103
pub struct UsageAgent {
113104
token: String,
@@ -257,15 +248,12 @@ impl UsageAgent {
257248
persistedDocumentHash: op.persisted_document_hash,
258249
metadata,
259250
});
260-
if !report.map.contains_key(&hash) {
261-
report.map.insert(
262-
hash,
263-
OperationMapRecord {
264-
operation: operation.operation,
265-
operationName: non_empty_string(op.operation_name),
266-
fields: operation.coordinates,
267-
},
268-
);
251+
if let std::collections::hash_map::Entry::Vacant(e) = report.map.entry(hash) {
252+
e.insert(OperationMapRecord {
253+
operation: operation.operation,
254+
operationName: non_empty_string(op.operation_name),
255+
fields: operation.coordinates,
256+
});
269257
}
270258
report.size += 1;
271259
}
@@ -309,7 +297,7 @@ impl UsageAgent {
309297
)
310298
.header(
311299
reqwest::header::USER_AGENT,
312-
format!("hive-apollo-router/{}", COMMIT.unwrap_or_else(|| "local")),
300+
format!("hive-apollo-router/{}", COMMIT.unwrap_or("local")),
313301
)
314302
.json(&report)
315303
.send()

packages/libraries/router/src/graphql.rs

Lines changed: 68 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ use graphql_tools::ast::FieldByNameExtension;
55
use graphql_tools::ast::TypeDefinitionExtension;
66
use graphql_tools::ast::TypeExtension;
77
use lru::LruCache;
8-
use md5;
98
use std::cmp::Ordering;
109
use std::collections::BTreeMap;
1110
use std::collections::HashMap;
@@ -48,7 +47,7 @@ pub fn collect_schema_coordinates(
4847
let mut visit_context = OperationVisitorContext::new(document, schema);
4948
let mut visitor = SchemaCoordinatesVisitor {};
5049

51-
visit_document(&mut visitor, &document, &mut visit_context, &mut ctx);
50+
visit_document(&mut visitor, document, &mut visit_context, &mut ctx);
5251

5352
if let Some(error) = ctx.error {
5453
Err(error)
@@ -88,11 +87,11 @@ pub fn collect_schema_coordinates(
8887
struct SchemaCoordinatesVisitor {}
8988

9089
impl SchemaCoordinatesVisitor {
91-
fn resolve_type_name<'a>(&self, t: Type<'a, String>) -> String {
90+
fn resolve_type_name(&self, t: Type<String>) -> String {
9291
match t {
93-
Type::NamedType(value) => return value,
94-
Type::ListType(t) => return self.resolve_type_name(*t),
95-
Type::NonNullType(t) => return self.resolve_type_name(*t),
92+
Type::NamedType(value) => value,
93+
Type::ListType(t) => self.resolve_type_name(*t),
94+
Type::NonNullType(t) => self.resolve_type_name(*t),
9695
}
9796
}
9897

@@ -118,19 +117,13 @@ impl SchemaCoordinatesVisitor {
118117

119118
visited_types.push(type_name.to_string());
120119

121-
let named_type = schema.type_by_name(&type_name);
120+
let named_type = schema.type_by_name(type_name);
122121

123-
match named_type {
124-
Some(named_type) => match named_type {
125-
TypeDefinition::InputObject(input_type) => {
126-
for field in &input_type.fields {
127-
let field_type = self.resolve_type_name(field.value_type.clone());
128-
self._resolve_references(schema, &field_type, visited_types);
129-
}
130-
}
131-
_ => {}
132-
},
133-
None => {}
122+
if let Some(TypeDefinition::InputObject(input_type)) = named_type {
123+
for field in &input_type.fields {
124+
let field_type = self.resolve_type_name(field.value_type.clone());
125+
self._resolve_references(schema, &field_type, visited_types);
126+
}
134127
}
135128
}
136129
}
@@ -143,7 +136,7 @@ impl<'a> OperationVisitor<'a, SchemaCoordinatesContext> for SchemaCoordinatesVis
143136
field: &Field<'static, String>,
144137
) {
145138
if ctx.is_corrupted() {
146-
return ();
139+
return;
147140
}
148141

149142
let field_name = field.name.to_string();
@@ -157,17 +150,14 @@ impl<'a> OperationVisitor<'a, SchemaCoordinatesContext> for SchemaCoordinatesVis
157150
if let Some(field_def) = parent_type.field_by_name(&field_name) {
158151
// if field's type is an enum, we need to collect all possible values
159152
let field_output_type = info.schema.type_by_name(field_def.field_type.inner_type());
160-
match field_output_type {
161-
Some(TypeDefinition::Enum(enum_type)) => {
162-
for value in &enum_type.values {
163-
ctx.schema_coordinates.insert(format!(
164-
"{}.{}",
165-
enum_type.name.as_str(),
166-
value.name
167-
));
168-
}
153+
if let Some(TypeDefinition::Enum(enum_type)) = field_output_type {
154+
for value in &enum_type.values {
155+
ctx.schema_coordinates.insert(format!(
156+
"{}.{}",
157+
enum_type.name.as_str(),
158+
value.name
159+
));
169160
}
170-
_ => {}
171161
}
172162
}
173163
} else {
@@ -185,22 +175,19 @@ impl<'a> OperationVisitor<'a, SchemaCoordinatesContext> for SchemaCoordinatesVis
185175
var: &graphql_tools::static_graphql::query::VariableDefinition,
186176
) {
187177
if ctx.is_corrupted() {
188-
return ();
178+
return;
189179
}
190180

191181
let type_name = self.resolve_type_name(var.var_type.clone());
192182
let type_def = info.schema.type_by_name(&type_name);
193183

194-
match type_def {
195-
Some(TypeDefinition::Scalar(scalar_def)) => {
196-
ctx.schema_coordinates
197-
.insert(format!("{}", scalar_def.name.as_str(),));
198-
return ();
199-
}
200-
_ => {}
184+
if let Some(TypeDefinition::Scalar(scalar_def)) = type_def {
185+
ctx.schema_coordinates
186+
.insert(scalar_def.name.as_str().to_string());
187+
return;
201188
}
202189

203-
if let Some(inner_types) = self.resolve_references(&info.schema, &type_name) {
190+
if let Some(inner_types) = self.resolve_references(info.schema, &type_name) {
204191
for inner_type in inner_types {
205192
ctx.input_types_to_collect.insert(inner_type);
206193
}
@@ -216,15 +203,15 @@ impl<'a> OperationVisitor<'a, SchemaCoordinatesContext> for SchemaCoordinatesVis
216203
arg: &(String, Value<'static, String>),
217204
) {
218205
if ctx.is_corrupted() {
219-
return ();
206+
return;
220207
}
221208

222209
if info.current_parent_type().is_none() {
223210
ctx.error = Some(anyhow!(
224211
"Unable to find parent type of '{}' argument",
225212
arg.0.clone()
226213
));
227-
return ();
214+
return;
228215
}
229216

230217
let type_name = info.current_parent_type().unwrap().name();
@@ -240,36 +227,33 @@ impl<'a> OperationVisitor<'a, SchemaCoordinatesContext> for SchemaCoordinatesVis
240227

241228
let arg_value = arg.1.clone();
242229

243-
match info.current_input_type() {
244-
Some(input_type) => {
245-
match input_type {
246-
TypeDefinition::Scalar(scalar_def) => {
247-
ctx.schema_coordinates.insert(scalar_def.name.clone());
248-
}
249-
_ => {
250-
let input_type_name = input_type.name();
251-
match arg_value {
252-
Value::Enum(value) => {
253-
let value_str = value.to_string();
254-
ctx.schema_coordinates.insert(
255-
format!("{input_type_name}.{value_str}").to_string(),
256-
);
257-
}
258-
Value::List(_) => {
259-
// handled by enter_list_value
260-
}
261-
Value::Object(_a) => {
262-
// handled by enter_object_field
263-
}
264-
Value::Variable(_) => {
265-
// handled by enter_variable_definition
266-
}
267-
_ => {}
230+
if let Some(input_type) = info.current_input_type() {
231+
match input_type {
232+
TypeDefinition::Scalar(scalar_def) => {
233+
ctx.schema_coordinates.insert(scalar_def.name.clone());
234+
}
235+
_ => {
236+
let input_type_name = input_type.name();
237+
match arg_value {
238+
Value::Enum(value) => {
239+
let value_str = value.to_string();
240+
ctx.schema_coordinates.insert(
241+
format!("{input_type_name}.{value_str}").to_string(),
242+
);
268243
}
244+
Value::List(_) => {
245+
// handled by enter_list_value
246+
}
247+
Value::Object(_a) => {
248+
// handled by enter_object_field
249+
}
250+
Value::Variable(_) => {
251+
// handled by enter_variable_definition
252+
}
253+
_ => {}
269254
}
270255
}
271256
}
272-
None => {}
273257
}
274258
}
275259
}
@@ -280,13 +264,8 @@ impl<'a> OperationVisitor<'a, SchemaCoordinatesContext> for SchemaCoordinatesVis
280264
ctx: &mut SchemaCoordinatesContext,
281265
_object_field: &(String, graphql_tools::static_graphql::query::Value),
282266
) {
283-
if let Some(input_type) = info.current_input_type() {
284-
match input_type {
285-
TypeDefinition::Scalar(scalar_def) => {
286-
ctx.schema_coordinates.insert(scalar_def.name.clone());
287-
}
288-
_ => {}
289-
}
267+
if let Some(TypeDefinition::Scalar(scalar_def)) = info.current_input_type() {
268+
ctx.schema_coordinates.insert(scalar_def.name.clone());
290269
}
291270
}
292271

@@ -297,7 +276,7 @@ impl<'a> OperationVisitor<'a, SchemaCoordinatesContext> for SchemaCoordinatesVis
297276
values: &Vec<Value<'static, String>>,
298277
) {
299278
if ctx.is_corrupted() {
300-
return ();
279+
return;
301280
}
302281

303282
if let Some(input_type) = info.current_input_type() {
@@ -487,7 +466,7 @@ impl<'s, T: Text<'s> + Clone> OperationTransformer<'s, T> for SortSelectionsTran
487466
directives: &[Directive<'s, T>],
488467
) -> TransformedValue<Vec<Directive<'s, T>>> {
489468
let mut next_directives = self
490-
.transform_list(&directives, Self::transform_directive)
469+
.transform_list(directives, Self::transform_directive)
491470
.replace_or_else(|| directives.to_vec());
492471
next_directives.sort_unstable_by(|a, b| self.compare_directives(a, b));
493472
TransformedValue::Replace(next_directives)
@@ -498,7 +477,7 @@ impl<'s, T: Text<'s> + Clone> OperationTransformer<'s, T> for SortSelectionsTran
498477
arguments: &[(T::Value, Value<'s, T>)],
499478
) -> TransformedValue<Vec<(T::Value, Value<'s, T>)>> {
500479
let mut next_arguments = self
501-
.transform_list(&arguments, Self::transform_argument)
480+
.transform_list(arguments, Self::transform_argument)
502481
.replace_or_else(|| arguments.to_vec());
503482
next_arguments.sort_unstable_by(|a, b| self.compare_arguments(a, b));
504483
TransformedValue::Replace(next_arguments)
@@ -509,7 +488,7 @@ impl<'s, T: Text<'s> + Clone> OperationTransformer<'s, T> for SortSelectionsTran
509488
variable_definitions: &Vec<VariableDefinition<'s, T>>,
510489
) -> TransformedValue<Vec<VariableDefinition<'s, T>>> {
511490
let mut next_variable_definitions = self
512-
.transform_list(&variable_definitions, Self::transform_variable_definition)
491+
.transform_list(variable_definitions, Self::transform_variable_definition)
513492
.replace_or_else(|| variable_definitions.to_vec());
514493
next_variable_definitions.sort_unstable_by(|a, b| self.compare_variable_definitions(a, b));
515494
TransformedValue::Replace(next_variable_definitions)
@@ -527,11 +506,11 @@ impl<'s, T: Text<'s> + Clone> OperationTransformer<'s, T> for SortSelectionsTran
527506
Transformed::Replace(FragmentDefinition {
528507
selection_set: SelectionSet {
529508
items: selections.replace_or_else(|| fragment.selection_set.items.clone()),
530-
span: fragment.selection_set.span.clone(),
509+
span: fragment.selection_set.span,
531510
},
532511
directives,
533512
name: fragment.name.clone(),
534-
position: fragment.position.clone(),
513+
position: fragment.position,
535514
type_condition: fragment.type_condition.clone(),
536515
})
537516
}
@@ -673,20 +652,16 @@ impl OperationProcessor {
673652
.into_static();
674653

675654
let is_introspection = parsed.definitions.iter().find(|def| match def {
676-
Definition::Operation(op) => match op {
677-
OperationDefinition::Query(query) => query
678-
.selection_set
679-
.items
680-
.iter()
681-
.find(|selection| match selection {
682-
Selection::Field(field) => {
683-
field.name == "__schema" || field.name == "__type"
684-
}
685-
_ => false,
686-
})
687-
.is_some(),
688-
_ => false,
689-
},
655+
Definition::Operation(OperationDefinition::Query(query)) => query
656+
.selection_set
657+
.items
658+
.iter()
659+
.any(|selection| match selection {
660+
Selection::Field(field) => {
661+
field.name == "__schema" || field.name == "__type"
662+
}
663+
_ => false,
664+
}),
690665
_ => false,
691666
});
692667

0 commit comments

Comments
 (0)