Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix clippy 1.45 suggestions for edgelet #3268

Merged
merged 2 commits into from
Jul 22, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 6 additions & 7 deletions edgelet/edgelet-core/src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,13 +114,12 @@ impl ManualDeviceConnectionString {
let mut device_id = None;
let mut hub = None;

let parts: Vec<&str> = self.device_connection_string.split(';').collect();
for p in parts {
let s: Vec<&str> = p.split('=').collect();
match s[0] {
SHAREDACCESSKEY_KEY => key = Some(s[1].to_string()),
DEVICEID_KEY => device_id = Some(s[1].to_string()),
HOSTNAME_KEY => hub = Some(s[1].to_string()),
for sections in self.device_connection_string.split(';') {
let mut parts = sections.split('=');
match parts.next() {
Some(SHAREDACCESSKEY_KEY) => key = parts.next().map(String::from),
Some(DEVICEID_KEY) => device_id = parts.next().map(String::from),
Some(HOSTNAME_KEY) => hub = parts.next().map(String::from),
_ => (), // Ignore extraneous component in the connection string
}
}
Expand Down
17 changes: 10 additions & 7 deletions edgelet/edgelet-core/src/watchdog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,9 @@ where
.stop(name, Some(EDGE_RUNTIME_STOP_TIME))
.or_else(|err| match (&err).into() {
ModuleRuntimeErrorReason::NotFound => Ok(()),
_ => Err(Error::from(err.context(ErrorKind::ModuleRuntime))),
ModuleRuntimeErrorReason::Other => {
Err(Error::from(err.context(ErrorKind::ModuleRuntime)))
}
})
}

Expand Down Expand Up @@ -139,15 +141,16 @@ where
})
})
.fold(0, move |exec_count: u32, result: Option<Error>| {
result
.and_then(|e| {
result.map_or_else(
|| Ok(0),
|e| {
if max_retries.compare(exec_count) == Ordering::Greater {
Some(Ok(exec_count + 1))
Ok(exec_count + 1)
} else {
Some(Err(e))
Err(e)
}
})
.unwrap_or_else(|| Ok(0))
},
)
})
.map(|_| ())
}
Expand Down
35 changes: 14 additions & 21 deletions edgelet/edgelet-docker/src/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,14 +199,12 @@ impl MakeModuleRuntime for DockerModuleRuntime {
_: impl GetTrustBundle,
) -> Self::Future {
info!("Initializing module runtime...");

// Clippy incorrectly flags the use of `.map(..).unwrap_or_else(..)` code as being replaceable
// with `.ok().map_or_else`. This is incorrect because `.ok()` will result in the error being dropped.
// So we suppress this lint. There's an open issue for this on the Clippy repo:
// https://github.com/rust-lang/rust-clippy/issues/3730
#[allow(clippy::result_map_unwrap_or_else)]
let created = init_client(settings.moby_runtime().uri())
.map(|client| {
let created = init_client(settings.moby_runtime().uri()).map_or_else(
|err| {
log_failure(Level::Warn, &err);
future::Either::B(Err(err).into_future())
},
|client| {
let network_id = settings.moby_runtime().network().name().to_string();
let (enable_i_pv6, ipam) = get_ipv6_settings(settings.moby_runtime().network());
info!("Using runtime network id {}", network_id);
Expand Down Expand Up @@ -253,11 +251,8 @@ impl MakeModuleRuntime for DockerModuleRuntime {
});

future::Either::A(fut)
})
.unwrap_or_else(|err| {
log_failure(Level::Warn, &err);
future::Either::B(Err(err).into_future())
});
},
);

Box::new(created)
}
Expand Down Expand Up @@ -334,7 +329,7 @@ impl ModuleRuntime for DockerModuleRuntime {
let result = module
.config()
.clone_create_options()
.and_then(|create_options| {
.map(|create_options| {
// merge environment variables
let merged_env = DockerModuleRuntime::merge_env(create_options.env(), module.env());

Expand All @@ -357,9 +352,7 @@ impl ModuleRuntime for DockerModuleRuntime {

// Here we don't add the container to the iot edge docker network as the edge-agent is expected to do that.
// It contains the logic to add a container to the iot edge network only if a network is not already specified.

Ok(self
.client
self.client
.container_api()
.container_create(create_options, module.name())
.then(|result| match result {
Expand All @@ -370,7 +363,7 @@ impl ModuleRuntime for DockerModuleRuntime {
module.name().to_string(),
)),
)),
}))
})
})
.into_future()
.flatten()
Expand Down Expand Up @@ -478,9 +471,9 @@ impl ModuleRuntime for DockerModuleRuntime {
}

#[allow(clippy::cast_possible_truncation, clippy::cast_sign_loss)]
let wait_timeout = wait_before_kill.and_then(|s| match s.as_secs() {
s if s > i32::max_value() as u64 => Some(i32::max_value()),
s => Some(s as i32),
let wait_timeout = wait_before_kill.map(|s| match s.as_secs() {
s if s > i32::max_value() as u64 => i32::max_value(),
s => s as i32,
});

Box::new(
Expand Down
4 changes: 2 additions & 2 deletions edgelet/edgelet-docker/tests/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ fn make_get_networks_handler(
}

fn make_create_network_handler(
on_post: impl Fn(Request<Body>) -> () + Clone + Send + 'static,
on_post: impl Fn(Request<Body>) + Clone + Send + 'static,
) -> impl Fn(Request<Body>) -> ResponseFuture + Clone {
move |req| {
on_post(req);
Expand Down Expand Up @@ -154,7 +154,7 @@ fn not_found_handler(_: Request<Body>) -> ResponseFuture {

fn make_network_handler(
on_get: impl Fn() -> String + Clone + Send + 'static,
on_post: impl Fn(Request<Body>) -> () + Clone + Send + 'static,
on_post: impl Fn(Request<Body>) + Clone + Send + 'static,
) -> impl Fn(Request<Body>) -> Box<dyn Future<Item = Response<Body>, Error = HyperError> + Send> + Clone
{
let dispatch_table = routes!(
Expand Down
6 changes: 3 additions & 3 deletions edgelet/edgelet-http-mgmt/src/client/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,11 +191,11 @@ impl ModuleRuntime for ModuleClient {
)
})
.then(|result| match result {
other @ Ok(_) => other,
Err(e) => match e.kind() {
ErrorKind::NotModified => Ok(()),
_ => Err(e),
},
other => other,
});
Box::new(start)
}
Expand All @@ -214,11 +214,11 @@ impl ModuleRuntime for ModuleClient {
)
})
.then(|result| match result {
other @ Ok(_) => other,
Err(e) => match e.kind() {
ErrorKind::NotModified => Ok(()),
_ => Err(e),
},
other => other,
});
Box::new(stop)
}
Expand All @@ -237,11 +237,11 @@ impl ModuleRuntime for ModuleClient {
)
})
.then(|result| match result {
other @ Ok(_) => other,
Err(e) => match e.kind() {
ErrorKind::NotModified => Ok(()),
_ => Err(e),
},
other => other,
});
Box::new(restart)
}
Expand Down
5 changes: 2 additions & 3 deletions edgelet/hsm-rs/src/crypto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ fn make_certification_props(props: &CertificateProperties) -> Result<CERT_PROPS_
CertificateType::Client => CERTIFICATE_TYPE_CERTIFICATE_TYPE_CLIENT,
CertificateType::Server => CERTIFICATE_TYPE_CERTIFICATE_TYPE_SERVER,
CertificateType::Ca => CERTIFICATE_TYPE_CERTIFICATE_TYPE_CA,
_ => CERTIFICATE_TYPE_CERTIFICATE_TYPE_UNKNOWN,
CertificateType::Unknown => CERTIFICATE_TYPE_CERTIFICATE_TYPE_UNKNOWN,
};
let result = unsafe { set_certificate_type(handle, c_cert_type) };
match result {
Expand Down Expand Up @@ -281,9 +281,8 @@ impl CreateCertificate for Crypto {

CString::new(alias)
.ok()
.and_then(|c_alias| {
.map(|c_alias| {
unsafe { if_fn(self.handle, c_alias.as_ptr()) };
Some(())
})
.ok_or_else(|| ErrorKind::ToCStr)?;
Ok(())
Expand Down
4 changes: 2 additions & 2 deletions edgelet/iotedge-proxy/src/routine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ fn start_api(
))
})
})
.and_then(move |addr| {
.map(move |addr| {
let new_service = ApiService::new();

let server = Server::bind(&addr)
Expand All @@ -109,7 +109,7 @@ fn start_api(
settings.entrypoint(),
);

Ok(server)
server
})
.into_future()
.flatten()
Expand Down
8 changes: 4 additions & 4 deletions edgelet/iotedged/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1617,9 +1617,9 @@ fn manual_provision_connection_string(
InitializeErrorReason::ManualProvisioningClient,
)))
})
.and_then(|k| {
.map(|k| {
let derived_key_store = DerivedKeyStore::new(k.clone());
Ok((derived_key_store, prov_result, k))
(derived_key_store, prov_result, k)
})
});
tokio_runtime.block_on(provision)
Expand Down Expand Up @@ -1775,9 +1775,9 @@ fn external_provision_tpm(
),
)))
})
.and_then(|k| {
.map(|k| {
let derived_key_store = DerivedKeyStore::new(k.clone());
Ok((derived_key_store, k))
(derived_key_store, k)
})
}

Expand Down
26 changes: 17 additions & 9 deletions edgelet/kube-client/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,9 @@ where
self.request(req, true)
.and_then(|response| match response {
ListResponse::Ok(list) => Ok(list),
_ => Err(Error::from(ErrorKind::Response(RequestType::ConfigMapList))),
ListResponse::Other(_) => {
Err(Error::from(ErrorKind::Response(RequestType::ConfigMapList)))
}
})
.map_err(|err| {
Error::from(err.context(ErrorKind::Response(RequestType::ConfigMapList)))
Expand Down Expand Up @@ -265,7 +267,7 @@ where
self.request(req, true)
.and_then(|response| match response {
ListResponse::Ok(deployments) => Ok(deployments),
_ => Err(Error::from(ErrorKind::Response(
ListResponse::Other(_) => Err(Error::from(ErrorKind::Response(
RequestType::DeploymentList,
))),
})
Expand Down Expand Up @@ -379,7 +381,9 @@ where
self.request(req, true)
.and_then(|response| match response {
ListResponse::Ok(list) => Ok(list),
_ => Err(Error::from(ErrorKind::Response(RequestType::PodList))),
ListResponse::Other(_) => {
Err(Error::from(ErrorKind::Response(RequestType::PodList)))
}
})
.map_err(|err| {
Error::from(err.context(ErrorKind::Response(RequestType::PodList)))
Expand All @@ -396,7 +400,9 @@ where
self.request(req, true)
.and_then(|response| match response {
ListResponse::Ok(list) => Ok(list),
_ => Err(Error::from(ErrorKind::Response(RequestType::NodeList))),
ListResponse::Other(_) => {
Err(Error::from(ErrorKind::Response(RequestType::NodeList)))
}
})
.map_err(|err| {
Error::from(err.context(ErrorKind::Response(RequestType::NodeList)))
Expand All @@ -422,7 +428,9 @@ where
self.request(req, false)
.and_then(|response| match response {
ListResponse::Ok(list) => Ok(list),
_ => Err(Error::from(ErrorKind::Response(RequestType::SecretList))),
ListResponse::Other(_) => {
Err(Error::from(ErrorKind::Response(RequestType::SecretList)))
}
})
.map_err(|err| {
Error::from(err.context(ErrorKind::Response(RequestType::SecretList)))
Expand Down Expand Up @@ -536,7 +544,7 @@ where
self.request(req, true)
.and_then(|response| match response {
ListResponse::Ok(list) => Ok(list),
_ => Err(Error::from(ErrorKind::Response(
ListResponse::Other(_) => Err(Error::from(ErrorKind::Response(
RequestType::ServiceAccountList,
))),
})
Expand Down Expand Up @@ -598,9 +606,9 @@ where
api_core::ReadNamespacedServiceAccountResponse::Ok(service_account) => {
Ok(service_account)
}
_ => Err(Error::from(ErrorKind::Response(
RequestType::ServiceAccountGet,
))),
api_core::ReadNamespacedServiceAccountResponse::Other(_) => Err(Error::from(
ErrorKind::Response(RequestType::ServiceAccountGet),
)),
})
.map_err(|err| {
Error::from(err.context(ErrorKind::Response(RequestType::ServiceAccountGet)))
Expand Down
2 changes: 1 addition & 1 deletion edgelet/systemd/src/linux.rs
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ mod tests {
match socket {
Socket::Inet(n, _) => unistd::close(n).unwrap(),
Socket::Unix(u) => unistd::close(u).unwrap(),
_ => (),
Socket::Unknown => (),
}
}
}
Expand Down